13

Possible Duplicate:
I have a nested data structure / JSON, how can I access a specific value?

I have a json object, like the one below:

[
  ["Blankaholm", "Gamleby"],
  ["2012-10-23", "2012-10-22"],
  ["Blankaholm. Under natten har det varit inbrott", "E22 i med Gamleby. Singelolycka. En bilist har.],
  ["57.586174","16.521841"], ["57.893162","16.406090"]
]

It consists of 4 "property levels" (city, date, description and coordinates).

What I want to do is to be able to access these levels like in an array like this:

var coordinates = jsonObject[4];

This does obvious not work so my question is how can I do it?

Do I need to decode it or something, and if so how?

9
  • 3
    Strictly speaking that's not a JSON object. That's an array of arrays.
    – nemesisx00
    Commented Jan 18, 2013 at 21:57
  • 3
    @NemesisX00 strictly speaking, it is perfectly valid JSON: stackoverflow.com/questions/5034444/can-a-json-start-with
    – George
    Commented Jan 18, 2013 at 21:59
  • 2
    @GeorgeP It's valid JSON. But not a JSON Object.
    – nemesisx00
    Commented Jan 18, 2013 at 22:04
  • 2
    @NemesisX00 There's no such thing as a JSON object.
    – natlee75
    Commented Jan 18, 2013 at 22:09
  • 1
    Is there a reason you're avoiding using the normal key:value syntax? It seems like it would be easy like jsonObj = [ { "city": "Gamleby", "date": "2012-10-23", "description": "...", "coordinates": [54.xx, 16.xx]}, {...}] Commented Jan 18, 2013 at 22:09

4 Answers 4

24

I found a straight forward way of solving this, with the use of JSON.parse.

Let's assume the json below is inside the variable jsontext.

[
  ["Blankaholm", "Gamleby"],
  ["2012-10-23", "2012-10-22"],
  ["Blankaholm. Under natten har det varit inbrott", "E22 i med Gamleby. Singelolycka. En bilist har.],
  ["57.586174","16.521841"], ["57.893162","16.406090"]
]

The solution is this:

var parsedData = JSON.parse(jsontext);

Now I can access the elements the following way:

var cities = parsedData[0];
4
  • 6
    +1 Ah, the one person on this site who understands what JSON is!
    – the system
    Commented Jan 18, 2013 at 22:14
  • 5
    ...oh, and it turns out to be the OP! :D
    – the system
    Commented Jan 18, 2013 at 22:14
  • The parsed data is identical to the raw data in this case (minus the missing double quote in the 3rd index). JSON.parse() would be used to convert the string representation of this object back into an object (or the result of JSON.stringify()) Commented Jan 18, 2013 at 22:30
  • 3
    @JeffreySweeney: OP received the JSON data as the response to an AJAX request, or from a cookie, or something similar, and needs to parse it. Raw data arrives as Unicode text, it gets parsed, it gets used in the program. This is the point of JSON. If it's part of a var statement (and not stored as a string), then it's not JSON. If it is stored as a string in a var, well then that would just be weird.
    – the system
    Commented Jan 18, 2013 at 22:39
4

The your seems a multi-array, not a JSON object.

If you want access the object like an array, you have to use some sort of key/value, such as:

var JSONObject = {
  "city": ["Blankaholm, "Gamleby"],
  "date": ["2012-10-23", "2012-10-22"],
  "description": ["Blankaholm. Under natten har det varit inbrott", "E22 i med Gamleby. Singelolycka. En bilist har.],
  "lat": ["57.586174","16.521841"], 
  "long": ["57.893162","16.406090"]
}

and access it with:

JSONObject.city[0] // => Blankaholm
JSONObject.date[1] // => 2012-10-22

and so on...

or

JSONObject['city'][0] // => Blankaholm
JSONObject['date'][1] // => 2012-10-22

and so on...

or, in last resort, if you don't want change your structure, you can do something like that:

var JSONObject = {
  "data": [
    ["Blankaholm, "Gamleby"],
    ["2012-10-23", "2012-10-22"],
    ["Blankaholm. Under natten har det varit inbrott", "E22 i med Gamleby. Singelolycka. En bilist har.],
    ["57.586174","16.521841"], 
    ["57.893162","16.406090"]
  ]
}

JSONObject.data[0][1] // => Gambleby
5
  • This method is also much more expandable/predictable. You can transverse fields in objects via for( in ) loops. Commented Jan 18, 2013 at 22:04
  • Yes @JeffreySweeney, in fact the first example is the one I would suggest for data structures like that.
    – Ragnarokkr
    Commented Jan 18, 2013 at 22:09
  • You're writing JavaScript data structures as part of a JavaScript program. The question states that the structure shown is JSON data, not a JavaScript program. The data needs to be parsed before it can be used.
    – the system
    Commented Jan 18, 2013 at 22:12
  • @thesystem I wrote it as JavaScript variable to make clear the example. If you remove the var jsonObject and put the data structure in a JSON string to parse, it's valid JSON.
    – Ragnarokkr
    Commented Jan 18, 2013 at 22:14
  • That doesn't make it clear if OP has JSON data. It makes it seem as though it doesn't need to be parsed.
    – the system
    Commented Jan 18, 2013 at 22:15
2

I noticed a couple of syntax errors, but other than that, it should work fine:

var arr = [
  ["Blankaholm", "Gamleby"],
  ["2012-10-23", "2012-10-22"],
  ["Blankaholm. Under natten har det varit inbrott", "E22 i med Gamleby. Singelolycka. En bilist har."], //<- syntax error here
  ["57.586174","16.521841"], ["57.893162","16.406090"]
];


console.log(arr[4]);    //["57.893162","16.406090"]
console.log(arr[4][0]); //57.893162
0
var coordinates = [jsonObject[3][0], 
                   jsonObject[3][0],
                   jsonObject[4][1], 
                   jsonObject[4][1]];

Not the answer you're looking for? Browse other questions tagged or ask your own question.