Append JSON Strings
This will do:
// Input:
var json_string = '{"name":"John"}';
// Convert JSON array to JavaScript object
var json_obj = JSON.parse( json_string );
// Add new key value pair "myData": "Helo World" to object
json_obj.myData = 'Hello World';
// Another more dynamic way to add new key/value pair
json_obj['myAnotherData'] = 'FooBar';
// Convert back to JSON string
json_string = JSON.stringify( json_obj );
// Log to console:
console.log( json_string );
Output:
{
"name": "John",
"myData": "Hello World",
"myAnotherData": "FooBar"
}
jQuery JSON parser:
If you want to use jQuery parser or need support for IE7 you can replace
var json_obj = JSON.parse( json_string );
with
var json_obj = $.parseJSON( json_string );
Unfortunately, jQuery cannot convert json_object
back to JSON string, you will need another plugin or library for that.
More on topic:
Browser support