3

I have a JSON string below that is being stored as a var in JavaScript. I am trying to parse the pieces of the string into variables.

In particular, I need the address, postcode, region, and locality.

This JSON array is being stored as a JS var called "data"

Does anyone know how I can begin parsing out those things? Thank you all!

[{"address":"2801 Elliott Ave","category_ids":[347],"category_labels":[["Social","Food and 
Dining","Restaurants"]],"country":"us","email":"kimd@thedussingroup.com","factual_id":"43cfe23
8-ae8e-469a-8592-a1edc8603051","fax":"(206) 448-
9252","latitude":47.615154,"locality":"Seattle","longitude":-122.353724,"name":"The Old 
Spaghetti Factory","neighborhood":["Belltown","Downtown","Downtown 
Seattle"],"postcode":"98121","region":"WA","tel":"(206) 441-
7724","website":"http:\/\/www.osf.com"}]

Appreciate the help!

Brandon
  • 1,701
  • 3
  • 16
  • 26
  • Just for the record, there was a proposal of destructuring bind in JavaScript, here's an example: http://stackoverflow.com/questions/204444 but it never made the list iirc. –  Oct 22 '13 at 09:13
  • Ah, and here's the link to the proposal itself: http://wiki.ecmascript.org/doku.php?id=proposals:destructuring_assignment –  Oct 22 '13 at 09:15

3 Answers3

3

You JSON is an array (since it's contained in [ and ]), so you need:

var data = JSON.parse('[{"addre....}]');
var address = data[0].address,
    postcode = data[0].postcode;

and so on...

kamituel
  • 34,606
  • 6
  • 81
  • 98
1
for(var i in data[0]){
    window[i] = data[0][i];
}

alert(address);
Krzysiek
  • 2,494
  • 16
  • 20
  • You should write one or two sentences and include a JSON parse statement to make this answer more clear. But I believe this is what the asker looking for. – Kemal Dağ Oct 22 '13 at 07:22
  • I think that user have already parsed JSON, moreover parsing JSON is a basic action, so in my opinion it's not necessary. – Krzysiek Oct 22 '13 at 07:27
  • User said that, he directly assigned JSON string to a var, this is not suggested way of doing it nor using an eval statement is. The correct way to use native JSON.parse or douglascrockford's json.js parser which is written in javascript and widely accepted and regarded as an standart. – Kemal Dağ Oct 22 '13 at 07:29
  • "This JSON array is being stored as a JS var called "data"" - array, not string. Maybe he used wrong words... – Krzysiek Oct 22 '13 at 07:31
  • it is clear that user did not used a conformative language as you can see that "I have a JSON string", so no need to argue on that. – Kemal Dağ Oct 22 '13 at 07:32
  • Ok, it's a little misunderstanding, but maybe someday someone will find this answer as useful as others ;) – Krzysiek Oct 22 '13 at 07:37
0

You can do this using JSON.parse

var data= your json;
JSON.parse(data);

Update: In your case you don'e even need to parse you can use directly like

console.log(data[0].address); //returns 2801 Elliott Ave 
console.log(data[0].category_ids); //returns [347] 

Check this JSFiddle

Praveen
  • 55,303
  • 33
  • 133
  • 164
  • Thanks! Would you mind maybe helping with an example? In the meantime I will look up usage of JSON.parse. Thanks! – Brandon Oct 22 '13 at 07:17
  • @user2905222 There no need to parse it, instead try like this `console.log(data[0].address);` – Praveen Oct 22 '13 at 07:24