Add value in JSON array in jquery

0
375
Add value in JSON array in jquery
Add value in JSON array in jquery

In this article, you going to see how one can add value to JSON array using jquery. To achieve this we go through step by step.

Let’s start with the topic step by step:

First important thing in adding of value is that we have to convert the jSON into string (JSON array in jquery).

To do that you should create a variable and then assign jSON string to that variable.

After creating that variable, then convert into array.

To converted into array use json decode function.

Now important pint is what is json decode and its syntax.

For converting the json object to php object json decode is used.

The syntax for json decode are as follow as.

json_decode(string, assoc, depth, options);
ParameterDescription
stringIt is required. Represent the value to be encoded.
assocIt is optional. Represent Boolean value.
depthIt is optional. Represent recursion depth.
optionsIt is a bitmask. Represent bitmask.
Parameter Values

After using json decode, it is converted into array.

$json_string = 
	'{
		"data" :{
			"id" : "1",
			"name" : "John",
			"number": "31",
			"city" : "New York"
		}
	}';
$my_array_data = json_decode($json_string, TRUE);
print_r($my_array_data);

From above example you come to know how to use this json decode

It will give the output are as follow as.

CONVERTED JSON TO ARRAY
Array
(
    [data] => Array
        (
            [id] => 1
            [name] => John
            [number] => 31
            [city] => New York
        )

)

Now our next point is how to add the value into this array.

Suppose you want to add color with key color.

With the use of below example you can achieve this.

$my_array_data['data']['color']='green';
print_r($my_array_data);

It will give output as follow as:

ADD VALUE TO ARRAY
Array
(
    [data] => Array
        (
            [id] => 1
            [name] => John
            [number] => 31
            [city] => New York
            [color] => green
        )

)

Now main and final point is to convert the array in which you have add the value.

To convert the array into json you can use json encode function.

Before using json encode, first discuss regarding what is json encode and its syntax.

To encoding value into json format the function is used called json encode function.

Below is syntax given for the json encode.

json_encode(value, options, depth)
ParameterDescription
valueIt is Required. It is the value to encoded
optionIt is optional. Represent a bitmask
depthit is optional. Represent the maximum depth
Parameter value.

With the use of json encode convert array into json.

Below is the example given for json encode.

json_encode($my_array_data); 

Pass the array variable in json encode function.

It will give the output are as follow as.

CONVERTED ARRAY TO JSON
{"data":{"id":"1","name":"John","number":"31","city":"New York","color":"green"}}

From above example you will completely understand how to add value to json array.

I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.