1

I am trying to read some values from a JSON object and use them to populate an html template. I can seem to figure out the right syntax to read from the JSON file. For example, when I want to read in the first object's "role", I write:

console.log("first object: "+ ((obj[0].roles)[0]).name)

The above works and returns "first object: thing commented on".

Then when I try to read the text of the verb under the "srcLanguageSentence" within the first object, I write:

console.log("first verb: "+ ((obj[0].srcLanguageSentence.verb[2]).text)

I expect to see "first verb: comment" but that does not happen. Where is my syntax wrong here?Please see a snippet of the JSON object file below:

[
  {
    "description": "",
    "roles": [
      {
        "name": "thing commented on"
      },
      {
        "name": "commentor"
      }
    ],
    "srcLanguageSentence": {
      "roles": [
        {
          "beginOffset": 23,
          "endOffset": 30,
          "name": "thing commented on",
          "text": "on them"
        },
        {
          "beginOffset": 5,
          "endOffset": 7,
          "name": "commentor",
          "text": "We"
        }
      ],
      "text": "  `` We wo n't comment on them . '' ",
      "verb": {
        "beginOffset": 15,
        "endOffset": 22,
        "text": "comment"
      }
    },
    "tgtLanguageSentences": [
      {
        "roles": [
          {
            "beginOffset": 1,
            "endOffset": 31,
            "name": "thing commented on",
            "text": "Weitere Aspekte der Kommission"
          },
          {
            "beginOffset": 44,
            "endOffset": 47,
            "name": "commentor",
            "text": "ich"
          },
          {
            "beginOffset": 48,
            "endOffset": 55,
            "name": "negation",
            "text": "nicht ."
          }
        ],
        "text": "  Weitere Aspekte der Kommission kommentiere ich nicht . ",
        "verb": {
          "beginOffset": -1,
          "endOffset": -1,
          "sense": "COMMENT, intransitive",
          "text": "kommentieren"
        }
      }
    ],
    "verb": "KOMMENTIEREN"
  },
]
HumaniTech
  • 93
  • 10

3 Answers3

4

Use:

console.log("first verb: "+ obj[0].srcLanguageSentence.verb.text)

Instead of:

console.log("first verb: "+ ((obj[0].srcLanguageSentence.verb[2]).text)

In general, you need to use the bracket notation [] when accessing an array and the dot notation (.something) when accessing a property of an object. In JSON, an array is indicated by [] while an object is indicated by {}. Those two simple rules make it easy to figure out how to access nested values typically seen in complex JSON objects.

Note that in practice, you can use bracket notation when access an object. For example, say we have this object:

var obj = { x: 10 };

I can do console.log(obj['x']) or console.log(obj.x) and either will work. This makes it possible to programmatically access an object. Say like this:

var obj = {
 x: 10,
 y: 30,
 z: -2
};

Object.keys(obj).forEach(function(key) {
 console.log(obj[key]);
});

There we loop over the keys (['x', 'y', 'z']) and can then use the bracket notation to access the values. We can't do obj.key because that would lookup key in the object -- not the value of key.

Cymen
  • 14,079
  • 4
  • 52
  • 72
1
console.log("first verb: "+ obj[0].srcLanguageSentence.verb.text)
vinayakj
  • 5,591
  • 3
  • 28
  • 48
0

console.log("first verb: "+ ((obj[0].srcLanguageSentence.verb[2]).text) should be:

console.log("first verb: "+ obj[0].srcLanguageSentence.verb.sense.text.split(', ')[0].toLowerCase());

That's if your desired output is first verb: comment.

Josh Beam
  • 19,292
  • 3
  • 45
  • 68