2

I have a json file that I'm trying to get values out of. One object is nested inside another in this file. I can isolate the top level values, but not the nested values. It must be a syntax issue. Here's what I'm using.

This is the json:

{
    "total": [
        [
            {
                "votes": "79,060"
            },
            {
                "percent": "11%"
            },
            {
                "winner": "0"
            }
        ],
        [
            {
                "votes": "167,800"
            },
            {
                "percent": "22%"
            },
            {
                "winner": "0"
            }
        ],
        [
            {
                "votes": "51,519"
            },
            {
                "percent": "7%"
            },
            {
                "winner": "0"
            }
        ],
        [
            {
                "votes": "297,060"
            },
            {
                "percent": "39%"
            },
            {
                "winner": "1"
            }
        ],
        [
            {
                "votes": "156,787"
            },
            {
                "percent": "21%"
            },
            {
                "winner": "0"
            }
        ]
    ],
    "useWinnerColors": 1,
    "timestamp": "3:00 p.m. April 26",
    "candidateCount": 5
}

When I write:

console.log(json.candidateCount);

I get the right answer (5).

But when I write:

console.log(json.total[0][1]);

I get Object { percent="11%"}.

And when I write:

console.log(json.total[0].votes);

I get undefined.

How do I isolate the value of the items in "total", please?

2
  • That is some strangely-constructed JSON. Why is total an array of 3-element arrays, each of which contains a one-element object, instead of total being an array of objects? Commented Jun 29, 2012 at 17:37
  • This is actually a snippet of a larger json file that has totals for each county. Commented Jun 29, 2012 at 17:39

2 Answers 2

6

You're getting undefined because json.total[0] is itself, an array. You need to isolate the specific array inside json.total[0]. So you would need to do something like json.total[0][0].votes or json.total[0][1].votes.

I think a better structure for your JSON would be something like this:

{"total": [    
   {
      "votes": "79,060"
      "percent": "11%"
      "winner": "0",
   },
   ...
   {
      "votes": "156,787",
      "percent": "21%",
      "winner": "0"
   }], 
   "useWinnerColors": 1,
   "timestamp": "3:00 p.m. April 26",
   "candidateCount": 5
}

Now you can do json.total[0].votes.

You don't need to create an array where each entry is a name-value pair. You can directly use an associative-array.

EDIT: To iterate over your associative array, use for..in along with hasOwnProperty(). The hasOwnProperty() check will prevent you from iterating over properties that it has inherited (some third-party libraries pollute the namespace):

var map = json.total[0];
for(var prop in map) if(map.hasOwnProperty(prop)) {
   var value = map[prop];
   ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

What if I don't know the name of the child node ("votes")? I see I can't use json.total[0][0][0]. How would I indicate that node?
Than you're trying to use hash without knowing its key, and it's not right. ) Strictly speaking, order of properties in object is not guaranteed, so even some imitation of iterating over them (with var p in o loop) will probably fail. And [0][0][0] is just not correct.
As raina77ow said, you should know the name. After all, you're the one who populated the hash :) If you don't know the name, what you can do is iterate over them. Although order is not guaranteed, you can access each property and its value. Check out my edit.
@VivinPaliath Where did map and prop come from? I'm trying to do something pretty similar but I'm not sure what values I need to put where.
@kgrote map and prop are just variable names that I used.
3

Each '[' symbol means we deal with Array, each '{' - with Object. So json.total is actually an array of arrays of objects (though each inner object is just a 'tuple' - single key-value pair.

So...

json.total[0][1] - evaluates to { 'percent': '11%' }

json.total[0]['votes'] - evaluates to nothing (undefined), as one step is skipped. ) It really should be json.total[0][n].votes or json.total[0][n]['votes']. These two forms are (almost) identical. )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.