javascript - How to append a attribute to already existing JSON using node js -


i have json 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 ubuntu 10.04 bitnami webpack 1.2-0 nova",           "description": "hpcloud large vm ubuntu 10.04 bitnami webpack 1.2-0 nova"         }       }       ] } 

i need manipulate attribute "metadata" follows (note new attribute 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 ubuntu 10.04 bitnami webpack 1.2-0 nova",           "description": "hpcloud large vm ubuntu 10.04 bitnami webpack 1.2-0 nova",           "charge" : 80         }       }       ] } 

any straightforward way accomplish using node js?

assuming mean javascript object, use this:

obj.elements[0].metadata.charge = 80; 

if mean json, parse before , encode afterwards again:

obj = json.parse( json ); obj.elements[0].metadata.charge = 80; json = json.stringify( obj ); 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -