I have a JSON as follows:
{
"elements": [
{
"name": "F975CFAF-8FB4-2926-DD56-74CD230D15AF",
"uri": "vm/hpcloud/nova/large",
"parameters": {
"imageUri": "image/hpcloud/nova/ami-00001b03",
"securityGroups": [
"default"
]
},
"metadata": {
"name": "HPCloud Large VM with Ubuntu 10.04 With BitNami WebPack 1.2-0 Nova",
"description": "HPCloud Large VM with Ubuntu 10.04 With BitNami WebPack 1.2-0 Nova"
}
}
]
}
I need to manipulate the attribute "metadata" as follows (note that new attribute is appended) :
{
"elements": [
{
"name": "F975CFAF-8FB4-2926-DD56-74CD230D15AF",
"uri": "vm/hpcloud/nova/large",
"parameters": {
"imageUri": "image/hpcloud/nova/ami-00001b03",
"securityGroups": [
"default"
]
},
"metadata": {
"name": "HPCloud Large VM with Ubuntu 10.04 With BitNami WebPack 1.2-0 Nova",
"description": "HPCloud Large VM with Ubuntu 10.04 With BitNami WebPack 1.2-0 Nova",
"charge" : 80
}
}
]
}
Any straightforward way to accomplish this using node js?
Assuming that you really mean a JavaScript object, use this:
obj.elements[0].metadata.charge = 80;
If you really mean JSON, than parse it before and encode it afterwards again:
obj = JSON.parse( json );
obj.elements[0].metadata.charge = 80;
json = JSON.stringify( obj );