0

How do I read JSON arrays with plain javascript?

Example of the array I need to read:

{
  "GetRouteSummaryForStopResult": {
    "StopNo": "1234",
    "Error": "",
    "StopDescription": "STREET 1 / STREET 2",
    "Routes": {
      "Route": {
        "RouteNo": "1234",
        "RouteHeading": "END_POINT",
        "DirectionID": 1,
        "Direction": "",
        "Trips": {
          "Trip": [
            {
              "Longitude": "-75",
              "Latitude": "45",
              "GPSSpeed": "",
              "TripDestination": "END_POINT",
              "TripStartTime": "10:30",
              "AdjustedScheduleTime": "9",
              "AdjustmentAge": "0.48",
              "LastTripOfSchedule": false,
              "BusType": "NFIN"
            },
            {
              "Longitude": "",
              "Latitude": "",
              "GPSSpeed": "",
              "TripDestination": "END_POINT",
              "TripStartTime": "11:00",
              "AdjustedScheduleTime": "29",
              "AdjustmentAge": "-1",
              "LastTripOfSchedule": false,
              "BusType": ""
            }
          ]
        }
      }
    }
  }
}

I am trying to read Longitude And Latitude To Place in a map.

How would I go about doing this? It is pulled from an external API

I have tried:

function on_page_ld() {
var parse_data = "api.example.com/api_name/?key=abcdefg12345"
var jsonData = JSON.parse();
for (var i = 1; i < jsonData.Routes.Route.RouteNo; i++) {
    var array = jsonData.Routes.Route.RouteNo[i];
    console.log(array);
  }
}

And I get:

VM4059:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0
2
  • 1
    I see two problems 1) var parse_data = "api.example.com/api_name/?key=abcdefg12345" - you are not downloading this data 2) var jsonData = JSON.parse(); - in your JSON.parse method you need to pass the data that you downloaded in previous line Commented Nov 22, 2021 at 18:01
  • stackoverflow.com/questions/46613243/… Checkout this post on SO. Looks like you are passing undefined and that is why it is parsing u at position 0 Commented Nov 22, 2021 at 18:01

2 Answers 2

1
async function on_page_ld(){
// Fetching data from API
const response = await fetch("api.example.com/api_name/?key=abcdefg12345")
// Parsing JSON
const data = await response.json()

// Your array of latitudes and longitudes
const locations = data.GetRouteSummaryForStopResult.Routes.Route.Trips.Trip

// get all your latitudes and longitudes 
for(i = 0; i < locations.length; i++){
 console.log(i + ") latitude :" + locations[i].latitude);
console.log(i + ") longitude  :" + locations[i].longitude);
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This helped me alot!
-1

You can read the values using a for-in loop

for (i in locations.length) {
  console.log(i + ") latitude :" + locations[i].latitude);
  console.log(i + ") longitude  :" + locations[i].longitude);
} 

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.