skip to Main Content

I’m trying to extract data from this API and this is what the structure looks like:

{
  "wait_time": {
    "port": [
      {
        "port_id": "240201",
        "wait_time_event": {
          "update_time": "2024-08-20T15:40:00",
          "lane": [
            {
              "type": "Commercial",
              "sub_type": "Aduana",
              "actual_delay": "31",
              "expected_delay": "31"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP FAST",
              "actual_delay": "5",
              "expected_delay": "6"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP Standard",
              "actual_delay": "7",
              "expected_delay": "8"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS FAST",
              "actual_delay": "7",
              "expected_delay": "6"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS Standard",
              "actual_delay": "7",
              "expected_delay": "7"
            }
          ]
        }
      },
      {
        "port_id": "535502",
        "wait_time_event": {
          "update_time": "2024-08-20T15:40:00",
          "lane": [
            {
              "type": "Commercial",
              "sub_type": "Aduana",
              "actual_delay": "5",
              "expected_delay": "5"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP FAST",
              "actual_delay": "13",
              "expected_delay": "13"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP Standard",
              "actual_delay": "74",
              "expected_delay": "68"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS FAST",
              "actual_delay": "5",
              "expected_delay": "5"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS Standard",
              "actual_delay": "4",
              "expected_delay": "5"
            }
          ]
        }
      },
      {
        "port_id": "230403",
        "wait_time_event": {
          "update_time": "2024-08-20T15:40:00",
          "lane": [
            {
              "type": "Commercial",
              "sub_type": "Aduana",
              "actual_delay": "8",
              "expected_delay": "7"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP FAST",
              "actual_delay": "5",
              "expected_delay": "5"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP Standard",
              "actual_delay": "21",
              "expected_delay": "20"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS FAST",
              "actual_delay": "4",
              "expected_delay": "4"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS Standard",
              "actual_delay": "4",
              "expected_delay": "4"
            }
          ]
        }
      },
      {
        "port_id": "230302",
        "wait_time_event": {
          "update_time": "2024-08-20T15:40:00",
          "lane": [
            {
              "type": "Commercial",
              "sub_type": "Aduana",
              "actual_delay": "1",
              "expected_delay": "1"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP FAST",
              "actual_delay": "1",
              "expected_delay": "1"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP Standard",
              "actual_delay": "5",
              "expected_delay": "5"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS FAST",
              "actual_delay": "2",
              "expected_delay": "2"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS Standard",
              "actual_delay": "4",
              "expected_delay": "4"
            }
          ]
        }
      },
      {
        "port_id": "250602",
        "wait_time_event": {
          "update_time": "2024-08-20T15:30:00",
          "lane": [
            {
              "type": "Commercial",
              "sub_type": "Aduana",
              "actual_delay": "0",
              "expected_delay": "0"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP FAST",
              "actual_delay": "8",
              "expected_delay": "9"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP Standard",
              "actual_delay": "11",
              "expected_delay": "12"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS FAST",
              "actual_delay": "0",
              "expected_delay": "0"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS Standard",
              "actual_delay": "0",
              "expected_delay": "0"
            }
          ]
        }
      },
      {
        "port_id": "230502",
        "wait_time_event": {
          "update_time": "2024-08-20T15:40:00",
          "lane": [
            {
              "type": "Commercial",
              "sub_type": "Aduana",
              "actual_delay": "3",
              "expected_delay": "3"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP FAST",
              "actual_delay": "8",
              "expected_delay": "8"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP Standard",
              "actual_delay": "8",
              "expected_delay": "8"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS FAST",
              "actual_delay": "12",
              "expected_delay": "11"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS Standard",
              "actual_delay": "20",
              "expected_delay": "19"
            }
          ]
        }
      },
      {
        "port_id": "230404",
        "wait_time_event": {
          "update_time": "2024-08-20T15:40:00",
          "lane": [
            {
              "type": "Commercial",
              "sub_type": "Aduana",
              "actual_delay": "65",
              "expected_delay": "55"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP FAST",
              "actual_delay": "12",
              "expected_delay": "21"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP Standard",
              "actual_delay": "17",
              "expected_delay": "18"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS FAST",
              "actual_delay": "0",
              "expected_delay": "0"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS Standard",
              "actual_delay": "30",
              "expected_delay": "24"
            }
          ]
        }
      },
      {
        "port_id": "240203",
        "wait_time_event": {
          "update_time": "2024-08-20T15:40:00",
          "lane": [
            {
              "type": "Commercial",
              "sub_type": "Aduana",
              "actual_delay": "11",
              "expected_delay": "14"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP FAST",
              "actual_delay": "19",
              "expected_delay": "12"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP Standard",
              "actual_delay": "26",
              "expected_delay": "26"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS FAST",
              "actual_delay": "14",
              "expected_delay": "18"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS Standard",
              "actual_delay": "7",
              "expected_delay": "6"
            }
          ]
        }
      },
      {
        "port_id": "535503",
        "wait_time_event": {
          "update_time": "2024-08-20T15:40:00",
          "lane": [
            {
              "type": "Commercial",
              "sub_type": "Aduana",
              "actual_delay": "12",
              "expected_delay": "8"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP FAST",
              "actual_delay": "10",
              "expected_delay": "10"
            },
            {
              "type": "Commercial",
              "sub_type": "CBP Standard",
              "actual_delay": "10",
              "expected_delay": "10"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS FAST",
              "actual_delay": "10",
              "expected_delay": "10"
            },
            {
              "type": "Commercial",
              "sub_type": "DPS Standard",
              "actual_delay": "10",
              "expected_delay": "10"
            }
          ]
        }
      }
    ]
  }
}

When I want to print the data to the console I get this:

"230404"
{
  wait_time: {
    port: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ]
  }
}

I want to acess the actual delay in port_id. This is what my code looks like to print the whole structure, but I’m not sure how to access the data:

const url=
    ' https://bciswebapi.azurewebsites.net/api/segmentcommercial'

//https://bciswebapi.azurewebsites.net/api/segmentpassenger
//'https://bwt.cbp.gov/api/waittimes'

async function getData(){
    const response = await fetch(url)
    const data= await response.json()
        .then(data=>console.log(data))


}

getData()

2

Answers


  1. If you want to extract all data from API on the log screen. You can use function util.inspect() when logging data. console.log(util.inspect(data,{showHidden: false, depth: null}))

    Login or Signup to reply.
  2. If your goal is to simply print the data, then you can do console.log(JSON.stringify(data, null, 2))

    But if your goal is to find the data for a specific port, then you have to do this, data.wait_time?.port.find(p => p.port_id === "230404")

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search