Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add item to JSON string

I create JSON object as:

var myJsonObject = JSON.stringify(objectString)

How I can add another item into myJsonObject??

like image 545
Valeriane Avatar asked Jan 28 '13 16:01

Valeriane


People also ask

How do you add something to a JSON object?

To append json data/elements to a json file, read the file, parse the JSON, push the new result to the array, convert into a string, and save it.

What is [] and {} in JSON?

' { } ' used for Object and ' [] ' is used for Array in json.


1 Answers

Looks like you're re-serializing the string rather than parsing it.

var myJsonObject = JSON.parse(objectString);

then you can add a new item by using

myJsonObject['newItemName'] = newValue;

Hope that's clear.

like image 145
ChrisIPowell Avatar answered Nov 07 '22 00:11

ChrisIPowell