0

I'm trying to pull information from a parsed JSON array i retrieved from a shipping company. I'm trying to retrieve the short_name of Cleveland, OH but every path I have tried to display that information has failed. If I do

response.write("</br>" + " SHORT HAND " + array.records[0].graph.nodes[0].short_name + "</br>");  

It will give me Los Angeles, CA but changing the node index to '1' gives me undefined. If I change short name to id I can get index of it every time. What is the pathing to get the short_name information from any index I want? Below is an expert from the output. I cant put it all here considering its over 30 pages long. Any advice is helpful.

{
  "records": [{
    "id": 451193,
    "flex_id": "FLEX-451193",
    "name": "PO#SW31",
    "transportation_mode": "ocean",
    "freight_type": "port_to_door",
    "incoterm": "FOB",
    "master_bill_number": null,
    "house_bill_number": "",
    "metric_units": true,
    "chargeable_volume": "65.0",
    "calculated_weight": "19000.0",
    "calculated_volume": "65.0",
    "departure_date": "2018-12-10T12:00:00.000+08:00",
    "arrival_date": "2019-01-02T12:00:00.000-05:00",
    "target_delivery_date": null,
    "status": "Seller's Location",
    "archived_at": null,
    "total_weight": "19000.0",
    "total_volume": "65.0",
    "chargeable_weight": "19000.0",
    "pieces": "740",
    "origin_pickup_date": null,
    "pickup_date": null,
    "delivery_date": null,
    "cargo_ready_date": "2018-12-07",
    "container_type": "high_cubed",
    "containers": [],
    "commercial_invoices": [],
    "graph": {
      "nodes": [{
        "id": 5851211,
        "type": "Port",
        "name": "Los Angeles, CA",
        "iata_code": null,
        "icao_code": null,
        "lat": "33.734048",
        "lng": "-118.257608",
        "port_name": "Los Angeles, CA",
        "port_code": "2704",
        "customs_district_code": 27,
        "country_code": "US",
        "timezone": "America/Los_Angeles",
        "short_name": "Los Angeles, CA",
        "city_state_country": "Los Angeles, CA, United States",
        "airport": false,
        "aliases": ["Los Angeles", "Los Angeles, CA", "San Pedro, CA", "LOS ANGELES (CALIFORNIA), UNITED STATES"],
        "city": "Los Angeles",
        "tags_list": ["port_of_unloading", "fmc_port_of_unloading"]
      }, {
        "address": "Choice Cabinet (Primary Warehouse)",
        "id": 5836273,
        "tags_list": ["destination_address"]
      }, {
        "address": "Yangming Industrial Park (Wang Old Factory)",
        "id": 5836272,
        "tags_list": ["origin_address"]
      }, {
        "id": 5836271,
        "type": "Port",
        "name": "Cleveland, OH",
        "iata_code": null,
        "icao_code": null,
        "lat": "41.416371",
        "lng": "-81.576069",
        "port_name": "Cleveland, OH",
        "port_code": "4101",
        "customs_district_code": 41,
        "country_code": "US",
        "timezone": "America/New_York",
        "short_name": "Cleveland, OH",
        "city_state_country": "Cleveland, OH, United States",
        "airport": false,
        "aliases": ["Cleveland", "CLEVELAND", "CLEVELAND, US", "CLEVELAND, OH"],
        "city": "Cleveland",
        "tags_list": ["customs_entry", "place_of_delivery"]
      }, {
        "id": 5836269,
        "type": "Port",
        "name": "Shanghai, China",
        "iata_code": null,
        "icao_code": null,
        "lat": "31.230418",
        "lng": "121.473701",
        "port_name": "Shanghai, China",
        "port_code": "57035",
        "customs_district_code": null,
        "country_code": "CN",
        "timezone": "Asia/Shanghai",
        "short_name": "Shanghai, China",
        "city_state_country": "Shanghai , China",
        "airport": false,
        "aliases": ["SHANGHAI (SHANGHAI), CHINA", "Shanghai"],
        "city": "Shanghai ",
        "tags_list": ["port_of_loading"]
      }],
      "legs": [{
        "origin_id": 5851211,
        "destination_id": 5836271,
        "transportation_mode": "rail",
        "freight_carrier_id": 2255,
        "tracking_number": null,
        "vessel_name": null,
        "vessel_imo": null,
        "scheduled_departure": "2018-12-25T12:00:00.000-08:00",
        "scheduled_arrival": "2019-01-02T12:00:00.000-05:00",
        "actual_departure": null,
        "actual_arrival": null
      }, {
        "origin_id": 5836269,
        "destination_id": 5851211,
        "transportation_mode": "ocean",
        "freight_carrier_id": 2255,
        "tracking_number": "075E",
        "vessel_name": "HANNOVER BRIDGE",
        "vessel_imo": "9302138",
        "scheduled_departure": "2018-12-10T12:00:00.000+08:00",
        "scheduled_arrival": "2018-12-24T12:00:00.000-08:00",
        "actual_departure": null,
        "actual_arrival": null
      }, {
        "origin_id": 5836271,
        "destination_id": 5836273,
        "transportation_mode": "truck",
        "freight_carrier_id": 2018,
        "tracking_number": null,
        "vessel_name": null,
        "vessel_imo": null,
        "scheduled_departure": null,
        "scheduled_arrival": null,
        "actual_departure": null,
        "actual_arrival": null
      }, {
        "origin_id": 5836272,
        "destination_id": 5836269,
        "transportation_mode": "truck"
      }]
    },
    "freight_cost": 0,
    "customs_cost": "125.0",
    "origin_address": "Yangming Industrial Park (Wang Old Factory), Shanghai Timber I/E, Xuanxing, Jiangsu, China",
    "destination_address": "Choice Cabinet (Primary Warehouse), Choice Cabinet, Bedford Heights, OH, United States"
  }]
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Brandon Reale
  • 39
  • 2
  • 11
  • *"but changing the node index to '1' gives me undefined."* If you look at your data you will see that `nodes[1]` refers to the object with `address` `"Choice Cabinet (Primary Warehouse)"`. That object doesn't have a `short_name`, which is why you get `undefined`. In other words, not every object in the `nodes` array has a `short_name`. Depending on what exactly you want to do, you might have to code defensively to account for such cases. – Felix Kling Nov 26 '18 at 18:54
  • This will work, but maybe you want to think about some programming logic instead of just hardcoding it. `array.records[0].graph.nodes[3].short_name`; – lloydaf Nov 26 '18 at 18:59

1 Answers1

0

Your data shows Cleveland here: array.records[0].graph.nodes[3].short_name

manonthemat
  • 6,101
  • 1
  • 24
  • 49