Accessing parent node in a JSON object

10,479

you can try the new JSON SELECT engine, looks interesting and affords CSS-like select statements over your JSON.

http://jsonselect.org/#overview

having said that, you can reference it differently like so: http://jsfiddle.net/dimitar/VVAFM/4/

the title var will refer to main title, always.

good luck :)

Share:
10,479
Starboy
Author by

Starboy

Updated on June 28, 2022

Comments

  • Starboy
    Starboy almost 2 years

    Having problems calling my parent Title ("Main title") in my JSON object. Below is a fiddle with what I currently have. (I can get it to call all child nodes in items, but again need to figure out how to call "title: Main Title")

    http://jsfiddle.net/VVAFM/3/

    Current JSON Tree

    {
        "title": "Main Title",
        "items": [{
            "title": "Sub Title 1 "
        },
        {
            "title": "Sub Title 2",
            "items": [{
                "title": "Sub Item 1"
            },
            {
                "title": "Sub Item 2"
            }]
        },
        {
            "title": "Sub Title 3",
            "items": [{
                "title": "Sub Item 1"
            },
            {
                "title": "Sub Item 2"
            }]
        },
        {
            "title": "Sub Title 4 "
        }]
    }
    
  • Starboy
    Starboy over 12 years
    So in that same context, if I were to call the title "Sub Item" would it be items.items.items.title?
  • Dimitar Christoff
    Dimitar Christoff over 12 years
    well, it will/can be recursive, won't it. you can pass it the whole json bean or a part of it, but each grouping that you do pass will have 2 properties: title: "string", items: [].