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?

Community
  • 1
  • 1
holyredbeard
  • 19,619
  • 32
  • 105
  • 171
  • 3
    Strictly speaking that's not a JSON object. That's an array of arrays. – NemesisX00 Jan 18 '13 at 21:57
  • 3
    @NemesisX00 strictly speaking, it is perfectly valid JSON: http://stackoverflow.com/questions/5034444/can-a-json-start-with – George Jan 18 '13 at 21:59
  • First, fix your JSON markup to make it valid. Then yes, because JSON is data serialization, it needs to be parsed to be used as native data structures for a given programming language. Lots of information out there on how to parse/decode JSON. – the system Jan 18 '13 at 22:00
  • @NemesisX00: Except for the missing quotation marks, it's valid JSON markup. It could only be an array of arrays if it's originating as part of a JavaScript program. There's nothing in the question do indicate that it is. – the system Jan 18 '13 at 22:04
  • 2
    @GeorgeP It's valid JSON. But not a JSON Object. – NemesisX00 Jan 18 '13 at 22:04
  • 2
    @NemesisX00 There's no such thing as a JSON object. – natlee75 Jan 18 '13 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]}, {...}]` – Austin Mullins Jan 18 '13 at 22:09
  • It's not entirely clear to me how you're trying to use this data. Could provide a more concrete example of code you would use to interact with this? – natlee75 Jan 18 '13 at 22:10
  • @Austin Why would you need to even wrap that in an array structure? – natlee75 Jan 18 '13 at 22:11

4 Answers4

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];
holyredbeard
  • 19,619
  • 32
  • 105
  • 171
  • 6
    +1 Ah, the one person on this site who understands what JSON is! – the system Jan 18 '13 at 22:14
  • 5
    ...oh, and it turns out to be the OP! :D – the system Jan 18 '13 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()`) – Jeffrey Sweeney Jan 18 '13 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 Jan 18 '13 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
Ragnarokkr
  • 2,328
  • 2
  • 21
  • 31
  • This method is also much more expandable/predictable. You can transverse fields in objects via `for( in )` loops. – Jeffrey Sweeney Jan 18 '13 at 22:04
  • Yes @JeffreySweeney, in fact the first example is the one I would suggest for data structures like that. – Ragnarokkr Jan 18 '13 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 Jan 18 '13 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 Jan 18 '13 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 Jan 18 '13 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
Jeffrey Sweeney
  • 5,986
  • 5
  • 24
  • 32
0
var coordinates = [jsonObject[3][0], 
                   jsonObject[3][0],
                   jsonObject[4][1], 
                   jsonObject[4][1]];
Paolo
  • 15,233
  • 27
  • 70
  • 91