0

I am trying to get the coordinates from a JSON file.

JSON File Structure:

    {
  "parkeerlocaties": [
    {
      "parkeerlocatie": {
        "Locatie": "{\"type\":\"Point\",\"coordinates\":[4.9032801,52.3824545]}"
      }
    }

I can access the Locatie object and it returns the following:

{"type":"Point","coordinates":[4.9032801,52.3824545]}

But when I am trying to get the coordinates from the JSON file, I get undefined back.

Code:

$.getJSON(parkingUrl, function(parkingData) {
                pData = parkingData.parkeerlocaties;
                for (var x = 0; x < pData.length; x++ ) {
                  ploc = parkingData["parkeerlocaties"][x]["parkeerlocatie"]["Locatie"]["coordinates"];
                  console.log(ploc);
                };
            });
      };

So, how can I get the coordinates from the JSON file?

4 Answers 4

3

Your Locatie key points to a string, not an object. You can use JSON.parse() to convert this string into an object so that you can then access the coordinates key.

ploc = JSON.parse(parkingData["parkeerlocaties"][x]["parkeerlocatie"]["Locatie"])["coordinates"];

So you code should look like:

$.getJSON(parkingUrl, function(parkingData) {
  pData = parkingData.parkeerlocaties;
  for (var x = 0; x < pData.length; x++) {
    ploc = JSON.parse(parkingData["parkeerlocaties"][x]["parkeerlocatie"]["Locatie"])["coordinates"];
    console.log(ploc);
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this worked! I tried the JSON.parse before, but I placed the parentheses wrong after coordinates, instead of infront of it.
2

You should use JSON.parse to convert the JSON string to JSON object.

JSON.parse(parkingData.parkeerlocaties[0].parkeerlocatie.Locatie).coordinates

Here Locatie is a JSON string.

$.getJSON(parkingUrl, function(parkingData) {
            pData = parkingData.parkeerlocaties;
            for (var x = 0; x < pData.length; x++ ) {
              ploc  = JSON.parse(parkingData.parkeerlocaties[x].parkeerlocatie.Locatie).coordinates

              console.log(ploc);
            };
        });
  };

Comments

0

Try to save the file avoiding escape sequence "\"

{ "parkeerlocaties": [ { "parkeerlocatie": { "Locatie": {"type":"Point","coordinates":[4.9032801,52.3824545]} } }

1 Comment

Please do not answer with a comment/question. Understandably, your rep is too low to comment, but that still does not mean answers should be used to make comments as an alternative. It would be preferable if you deleted this.
0

I have added following code for your further reference. It will help you to understand your mistake. Your json String missing "}" and "]" at the end of json string. Better to used JSON formatted for validate your JSON. json validation

    try{
        var test = "{\"parkeerlocaties\": [{  \"parkeerlocatie\": {\"Locatie\": {\"type\":\"Point\",\"coordinates\":[4.9032801,52.3824545]}}}]}";
        var jsondata = JSON.parse(test);
        var pData = jsondata.parkeerlocaties;

        for (var x = 0; x < pData.length; x++ ) {
          var ploc = jsondata["parkeerlocaties"][x]["parkeerlocatie"]["Locatie"]["coordinates"];
          alert(ploc);
        };
   }catch(e){alert(e)}

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.