javascriptjqueryjsonapimta

Getting value from nested JSON


I am attempting to parse through the following JSON data and console.log all the times for stop_ids of a specific value. So when I would select the stop ID "L26S", the console would log each instance that the stop time occurs within the JSON. The stop times I would like are under trip_update.stop_time_update.arrival.time.low

I have tried to use the following method unsuccessfully (feed is the name of my JSON file), I believe I'm on the right path with this but I don't know how to continue. Any help would be appreciated.

var feed = {
  "entity": [{
    "id": "1",
    "is_deleted": false,
    "trip_update": {
      "trip": {
        "trip_id": "009250_L..S",
        "route_id": "L",
        "start_time": null,
        "start_date": "20170512",
        "schedule_relationship": null,
        ".nyct_trip_descriptor": {
          "train_id": "0L 0132+8AV/RPY",
          "is_assigned": true,
          "direction": 3
        }
      },
      "vehicle": null,
      "stop_time_update": [{
          "stop_sequence": null,
          "stop_id": "L26S",
          "arrival": {
            "delay": null,
            "time": {
              "low": 1494569453,
              "high": 0,
              "unsigned": false
            },
            "uncertainty": null
          },
          "departure": {
            "delay": null,
            "time": {
              "low": 1494569483,
              "high": 0,
              "unsigned": false
            },
            "uncertainty": null
          },
          "schedule_relationship": 0,
          ".nyct_stop_time_update": {
            "scheduled_track": "1",
            "actual_track": "1"
          }
        },
        {
          "stop_sequence": null,
          "stop_id": "L27S",
          "arrival": {
            "delay": null,
            "time": {
              "low": 1494569551,
              "high": 0,
              "unsigned": false
            },
            "uncertainty": null
          }
        }
      ]
    }
  }]
}
$.each(feed.entity[0].trip_update.stop_time_update[1], function(i, v) {
  if (v.stop_id == "L02N") {
    console.log(v.arrival.time.low);
    return;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


Solution

  • Fixed your JSON and code access

    1. You needed to access each .stop_time_update
    2. Your test did not match any stop times in the JSON you gave
    3. I wrapped in $(function() {}) to allow entity to be defined below the code in the snippet

    $(function() {
      $.each(feed.entity[0].trip_update.stop_time_update, function(_, v) {
        if (v.stop_id == "L27S") {
          console.log(v.arrival.time.low);
          return;
        }
      });
    });
    var feed = {
      "entity": [{
        "id": "1",
        "is_deleted": false,
        "trip_update": {
          "trip": {
            "trip_id": "009250_L..S",
            "route_id": "L",
            "start_time": null,
            "start_date": "20170512",
            "schedule_relationship": null,
            ".nyct_trip_descriptor": {
              "train_id": "0L 0132+8AV/RPY",
              "is_assigned": true,
              "direction": 3
            }
          },
          "vehicle": null,
          "stop_time_update": [{
              "stop_sequence": null,
              "stop_id": "L26S",
              "arrival": {
                "delay": null,
                "time": {
                  "low": 1494569453,
                  "high": 0,
                  "unsigned": false
                },
                "uncertainty": null
              },
              "departure": {
                "delay": null,
                "time": {
                  "low": 1494569483,
                  "high": 0,
                  "unsigned": false
                },
                "uncertainty": null
              },
              "schedule_relationship": 0,
              ".nyct_stop_time_update": {
                "scheduled_track": "1",
                "actual_track": "1"
              }
            },
            {
              "stop_sequence": null,
              "stop_id": "L27S",
              "arrival": {
                "delay": null,
                "time": {
                  "low": 1494569551,
                  "high": 0,
                  "unsigned": false
                },
                "uncertainty": null
              }
            }
          ]
        }
      }]
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>