Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to refernence JSON elements without names?

    Hello,

    --> I would like to access children of JSON elements without names.

    I do not mean the content is empty or null, but rather ...

    For example:

    Valid JSON:
    Code:
    {
        "kingdom": {
            "phylum": {
                "class1": {
                    "": {
                        "family1": "1492",
                        "family2": "1776"
                    }
                },
                "class2": "1111"
            }
        }
    }


    To print the content of "class2" (1111) to the console, the following works...
    Code:
    console.log(kingdom.phylum.class2);

    --> How would I print the content of "family2" (1776) to the console in the case where the child of "class1" (order) is missing?

    Thank you.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    How would I print the content of "family2" (1776) to the console in the case where the child of "class1" (order) is missing?
    since "family2" is a grandchild of "class1" (and this only has one child), then if the child is missing, there won’t be a grandchild.

    unless, of course, I totally misinterpret your structure.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    since "family2" is a grandchild of "class1" (and this only has one child), then if the child is missing, there won’t be a grandchild.

    unless, of course, I totally misinterpret your structure.
    I don't know exactly what you mean by "there won’t be a grandchild."

    For additional illustration, you can download the JSON file in the following tutorial containing the same type of structure:
    How to Make Choropleth Maps in D3 | Visually Blog
    http://election2008.s3.amazonaws.com/votes/us-pres.json

    Although it is a deeper structure than my simplified example, you will be able to find the empty item between "President" and "votes."

    Both the JSON in my post above and the one in the example validate.

    1)
    The author suggests the following syntax:
    Code:
    console.log(statejson.locals.Alabama.races.President[''].final);
    Expected result: 1701
    Result: undefined

    Others in the comments section experiencing the same problem suggest:
    2)
    Code:
    console.log(statejson.locals.Alabama.races.President.''.final);
    Expected result: 1701
    Result: missing name after . operator

    3)
    Code:
    console.log(statejson[0].locals.Alabama.races.President[''].final);
    Expected result: 1701
    Result: statejson[0] is undefined

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    1) works for me.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •