How do you add a field to a json in node.js? I've seen it done with arrays but I need a field.
So basically
{
"hello":"this is cool"
}
into
{
"hello":"this is cool",
"hi":"i know"
}
CodePudding user response:
If you want to do it to JSON, then you can do it like below.
let json = `{
"key": "value"
}`;
const obj = JSON.parse(json);
obj.website = "Stack Overflow";
json = JSON.stringify(obj);
console.log(json);
However, if you want to do it to a regular object, just simply run the code below.
const obj = {
key: "value",
};
obj.website = "Stack Overflow";
console.log(obj);
CodePudding user response:
You can do it like so
const json = {
"hello":"this is cool"
};
json['hi'] = "i know";
console.log(json);
If JSON is a string you can use JSON.parse() as per MDN web docs JSON.parse().