user824624
user824624

Reputation: 8080

How to interpret JSON field with '-' in node.js

I am using the node.js to interpret a JSON data, the data format is like this below

{
  "href": "https://localhost/light/0000293D",
  "i-object-metadata": [
    {
      "rel": "temperature",
      "val": "244"
    }
  ]
}

I can print the raw data using print (body) to interpret data all works except printing the field i-object-metadata

var obj = JSON.parse(body);
console.log(obj.items);   // works well
console.log(obj.i-object-metadata);   // error 

How could I interpret the JSON object like this i-object-metadata

Upvotes: 0

Views: 65

Answers (1)

Marc B
Marc B

Reputation: 360802

Can't use the object shorthand in this case, you'll have to use the array notation:

console.log(obj['i-object-metadata'].val); // 244

Upvotes: 6

Related Questions