0

In my JSON object I am trying to access the "name" field within "pos"

I have tried obj.tokens.pos.parent; obj[0].pos.parent;

How can I access the parent field of this object?

the object looks like

[
 {
  "tokens": [
  {
   "text": "call",
   "normalised": "call",
   "end": true,
   "start": true,
   "pos": {
    "name": "infinitive verb",
    "example": "eat",
    "parent": "verb",
    "tense": "present",
    "tag": "VBP"
  },
  "pos_reason": "lexicon",
  "analysis": {
   "word": "call",
   "next": null,
   "last": null,
   "form": "infinitive",
   "tense": "present",
    "which": {
     "name": "infinitive verb",
     "example": "eat",
     "parent": "verb",
     "tense": "present",
     "tag": "VBP"
  },
  "negative": false
 }
 }
 ]
}
] 
user3404290
  • 45
  • 1
  • 2
  • 10

4 Answers4

1

Here the result is in array format so you need to access array element like obj[0]

obj[0].tokens[0].pos.name
Rashmin Javiya
  • 5,173
  • 3
  • 27
  • 49
0

Try using: obj[0].tokens[0].pos.name

Andy
  • 49,085
  • 60
  • 166
  • 233
link
  • 2,480
  • 1
  • 16
  • 18
0

I'm not sure what exactly you want, but you can try this

obj[0]["tokens"][0]["pos"]["name"]
Mritunjay
  • 25,338
  • 7
  • 55
  • 68
0

This should work.

obj[0].tokens[0].pos.name

antoniovassell
  • 1,012
  • 14
  • 24
  • Are you sure? You simple do this for example: alert(obj[0].tokens[0].pos.parent);That worked for me. Assuming that you made var obj = [ .... all that you have up there. – antoniovassell Jun 27 '14 at 04:19
  • Actually that works. but the problem now is that I have multiple objects within tokens and I am trying to access each field by doing alert(obj[0].tokens[i].pos.parent) in a for loop but I only get the value from the first object – user3404290 Jun 27 '14 at 04:37
  • OK, could I see that for loop definition? – antoniovassell Jun 27 '14 at 04:40
  • for(var i = 0; i < obj.length; i++){ console.log(obj[0].tokens[i].pos.parent)} I think my condition is probably wrong. – user3404290 Jun 27 '14 at 04:42
  • OK, this will only loop 1 time, if you only have 1 object. "i < obj.length" ..... Understand? It would have to be "i < obj[0].tokens.length; ..." – antoniovassell Jun 27 '14 at 04:46