1

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?

1
  • Assuming x is a variable with this information, you could just do x.metadata.charge = 80. Commented Jul 31, 2013 at 12:39

1 Answer 1

5

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 );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.