2

I am a bit stuck parsing a JSON array when it is the only thing returned in a JSON response.

I know how to parse an array when it is a sub record of a json response but not as the only response.

For example, i know how to do data.streams.array[0].item for example but when there is no 'streams' part of that i am not sure what to do (basically it goes straight to array[0]

Sample code of what i have tried below

    $( document ).ready(function() {
console.log( "ready to explore" );
$("#getMessage").click(function(){
$("ul").empty();
$.getJSON("https://api.planningalerts.org.au/applications.js?key=<<KEY>>&postcode=2000", function(json){
  var test2 = JSON.stringify(json);
  alert(test2);
  var test = json.address; 
  alert(test);

Sample json resposne i am trying to parse (remembering it goes straight to array)

[{
"application": {
    "id": 609706,
    "council_reference": "1-3879101556",
    "address": "\"Harbour Plaza\" Lot 5, 25-29 Dixon Street, Haymarket 2000",
    "description": "Triple 8 Hotel - Liquor licence transfer",
    "info_url": "http://www.ilga.nsw.gov.au/liquor/application-noticeboard",
    "comment_url": "mailto:liquorapplications@olgr.nsw.gov.au?subject=Application%20Number:%201-3879101556",
    "lat": -33.877797,
    "lng": 151.203659,
    "date_scraped": "2016-01-06T01:01:59.000Z",
    "date_received": "2016-01-05",
    "on_notice_from": null,
    "on_notice_to": "2016-02-04",
    "no_alerted": 178,
    "authority": {
        "full_name": "NSW Independent Liquor and Gaming Authority"
    }
}
}, {
"application": {
    "id": 609709,
    "council_reference": "1-3879170112",
    "address": "World Square L 10 680 George St, Sydney 2000",
    "description": "Sydney Chinese New Year 2016 - Chinese Film Festival 15/02/2016 - Limited licence - special event",
    "info_url": "http://www.ilga.nsw.gov.au/liquor/application-noticeboard",
    "comment_url": "mailto:liquorapplications@olgr.nsw.gov.au?subject=Application%20Number:%201-3879170112",
    "lat": -33.8772162,
    "lng": 151.2068193,
    "date_scraped": "2016-01-06T01:01:59.000Z",
    "date_received": "2016-01-05",
    "on_notice_from": null,
    "on_notice_to": "2016-01-19",
    "no_alerted": 182,
    "authority": {
        "full_name": "NSW Independent Liquor and Gaming Authority"
    }
}
}]

Any help really appreciated :)

Cheers

EJW
  • 338
  • 3
  • 6
  • 18
user2058234
  • 126
  • 1
  • 13

1 Answers1

3

First off, you don't have to parse anything; jQuery will parse it before giving it to your callback. (For that reason, json probably isn't a great argument name; perhaps data or response or applications.)

To access its data, you do so just like any other JavaScript array (because that's what it is). For instance, the first entry is an object with a property called application which refers to an object with a property called address with the value "\"Harbour Plaza\" Lot 5, 25-29 Dixon Street, Haymarket 2000". So:

console.log(json[0].application.address); // "Harbour Plaza" Lot 5, 25-29 Dixon Street, Haymarket 2000

There's also a second entry, whose address is "World Square L 10 680 George St, Sydney 2000", so:

console.log(json[1].application.address); // World Square L 10 680 George St, Sydney 2000

Or loop through the array, for example:

json.forEach(function(entry) {
    console.log(entry.application.address);
});

shows

"Harbour Plaza" Lot 5, 25-29 Dixon Street, Haymarket 2000
World Square L 10 680 George St, Sydney 2000
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks very much :) I was familiar with Jquery etc but i was confused by the fact it returned the array only and not as a subset. – user2058234 Jan 06 '16 at 09:23