Valeriane Valeriane - 5 months ago 28
Javascript Question

Add item to JSON string

I create

JSON
object as:

var myJsonObject = JSON.stringify(objectString)


How I can add another item into
myJsonObject
??

Answer

myJsonObject is now a string you cannot add anything to it again until you change it back into a JSON object.

So you can technically do:

var myJsonObject = JSON.parse(myJsonObject); //change to obj
myJsonObject.somethingnew = true; //add something
myJsonObject = JSON.stringify(myJsonObject); //change back to string
Comments