Appending 2 or more json string Appending 2 or more json string json json

Appending 2 or more json string


var json_str1 = '{ "name" : "foo1" , "value" : "bar1" }'var json_str2 = '{ "name" : "foo2" , "value" : "bar2" }'full_json = [json_str1, json_str2].join(',');


Do this:

full_json = [json_str1, json_str2].join();

but if you want to keep it valid JSON, then do this:

full_json = "[" + [json_str1, json_str2].join() + "]";

Or better, don't create your JSON strings manually.

var json_str1 = { name : "foo1" , value : "bar1" }var json_str2 = { name : "foo2" , value : "bar2" }var full_json = JSON.stringify([json_str1, json_str2]);


You could put the json strings in an array, join them, then set this to the json hidden field val.

var json_str1 = "{ 'name' : 'foo1' , 'value' : 'bar1' }"var json_str2 = "{ 'name' : 'foo2' , 'value' : 'bar2' }"var full_json = [json_str1, json_str2].join(','))

So that full_json equals the string value:

{ 'name' : 'foo1' , 'value' : 'bar1' },{ 'name' : 'foo2' , 'value' : 'bar2' }

Then just:

$("#json_hidden_field").val(full_json)