Extract data from JSON

Hello, I would like to extract the bus schedule from the following data:

[
  {
    "type": "ETA",
    "version": "1.0",
    "generated_timestamp ": "2021-08-28T20:39:54+08:00",
    "data": [
      {
        "co": "CTB",
        "route": "930X",
        "dir": "I",
        "seq": 19,
        "stop": "003340",
        "dest_tc": "荃灣(愉景新城)",
        "dest_en": "Tsuen Wan (Discovery Park)",
        "eta": "2021-08-28T20:41:23+08:00",
        "rmk_tc": "",
        "eta_seq": 1,
        "dest_sc": "荃湾(愉景新城)",
        "rmk_en": "",
        "rmk_sc": "",
        "data_timestamp": "2021-08-28T20:39:08+08:00"
      },
      {
        "co": "CTB",
        "route": "930X",
        "dir": "I",
        "seq": 19,
        "stop": "003340",
        "dest_tc": "荃灣(愉景新城)",
        "dest_en": "Tsuen Wan (Discovery Park)",
        "eta": "2021-08-28T21:03:35+08:00",
        "rmk_tc": "",
        "eta_seq": 2,
        "dest_sc": "荃湾(愉景新城)",
        "rmk_en": "",
        "rmk_sc": "",
        "data_timestamp": "2021-08-28T20:39:08+08:00"
      },
      {
        "co": "CTB",
        "route": "930X",
        "dir": "I",
        "seq": 19,
        "stop": "003340",
        "dest_tc": "荃灣(愉景新城)",
        "dest_en": "Tsuen Wan (Discovery Park)",
        "eta": "2021-08-28T21:17:22+08:00",
        "rmk_tc": "",
        "eta_seq": 3,
        "dest_sc": "荃湾(愉景新城)",
        "rmk_en": "",
        "rmk_sc": "",
        "data_timestamp": "2021-08-28T20:39:08+08:00"
      },
      {
        "co": "CTB",
        "route": "930X",
        "dir": "O",
        "seq": 1,
        "stop": "003340",
        "dest_tc": "銅鑼灣(摩頓台)",
        "dest_en": "Causeway Bay (Moreton Terrace)",
        "eta": "2021-08-28T20:55:00+08:00",
        "rmk_tc": "",
        "eta_seq": 1,
        "dest_sc": "铜锣湾(摩顿台)",
        "rmk_en": "",
        "rmk_sc": "",
        "data_timestamp": "2021-08-28T20:39:08+08:00"
      },
      {
        "co": "CTB",
        "route": "930X",
        "dir": "O",
        "seq": 1,
        "stop": "003340",
        "dest_tc": "銅鑼灣(摩頓台)",
        "dest_en": "Causeway Bay (Moreton Terrace)",
        "eta": "2021-08-28T21:15:00+08:00",
        "rmk_tc": "",
        "eta_seq": 2,
        "dest_sc": "铜锣湾(摩顿台)",
        "rmk_en": "",
        "rmk_sc": "",
        "data_timestamp": "2021-08-28T20:39:08+08:00"
      },
      {
        "co": "CTB",
        "route": "930X",
        "dir": "O",
        "seq": 1,
        "stop": "003340",
        "dest_tc": "銅鑼灣(摩頓台)",
        "dest_en": "Causeway Bay (Moreton Terrace)",
        "eta": "2021-08-28T21:35:00+08:00",
        "rmk_tc": "",
        "eta_seq": 3,
        "dest_sc": "铜锣湾(摩顿台)",
        "rmk_en": "",
        "rmk_sc": "",
        "data_timestamp": "2021-08-28T20:39:08+08:00"
      }
    ]
  }
]

I have a REST sensor to get the data:

  - platform: rest
    resource: https://rt.data.gov.hk/v1/transport/citybus-nwfb/eta/CTB/003340/930x
    name: Bus Arrival 930X At DP
    value_template: '1'
    json_attributes_path: '$..data[?(@.dir=="O")]'
    json_attributes:
      - dir
      - dest_tc
      - eta
    scan_interval: 10

The problem is, the bus stop is a terminal station, the bus schedule containing “inbound” and “outbound” data. I would like to get the outbound scheduled time only. I am using $..data[?(@.dir=="O")] in value_template to filtered the outbound scheduled time successfully but for the further development, I would like to filter the scheduled time for eta_seq==2 and eta_seq==3 also.

I have tested in jsonpath, using $..data[?(@.dir=="O"&@.eta_seq==2)] can filter out the data which I wanted but it is not working in REST value_template. Is there any way to filter it out? Thanks a lot.

1 Like