I thought I was getting the hang of JSON extraction. Obviously not so much…
I’m trying to use the Transport for London api but it is returning data (excuse me if my terminology is wrong here) in an array with no top level name.
Some elements within that array are also arrays, again with no top level name
Rather than paste the entire api call response, here is a simplified example of what I mean showing some data for two lines each with a different number of ‘lineStatuses’.
It is all the ‘lineStatuses’ data that I am primarily interested in (at the moment).
First my sensor so far which just counts the disrupted lines for the state (with my failed attempts and capturing attributes removed
(The resource url will work for anyone if you care to get all the data…)
- platform: rest
resource: https://api.tfl.gov.uk/Line/Mode/dlr,elizabeth-line,overground,tube/Status
name: TfL All Lines Status
value_template: >
{% if value_json is defined %}
{% set ns = namespace (disrupted_line_count = 0) %}
{% for line in value_json %}
{% if line.lineStatuses[0].statusSeverityDescription != 'Good Service' %}
{% set ns.disrupted_line_count = ns.disrupted_line_count + 1 %}
{% endif %}
{% endfor %}
{{ ns.disrupted_line_count }}
{% else %}
unavailable
{% endif %}
[
{
"$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
"id": "london-overground",
"name": "London Overground",
"modeName": "overground",
"lineStatuses": [
{
"$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
"lineId": "london-overground",
"statusSeverityDescription": "Severe Delays",
"reason": "No service between Hackney Downs and Walthamstow Central. SEVERE DELAYS between Walthamstow Central and Chingford, while we make urgent repairs to the track at Clapton. GOOD SERVICE on all other London Overground routes. "
},
{
"$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
"lineId": "london-overground",
"statusSeverityDescription": "Part Suspended",
"reason": "No service between Hackney Downs and Walthamstow Central. SEVERE DELAYS between Walthamstow Central and Chingford, while we make urgent repairs to the track at Clapton. GOOD SERVICE on all other London Overground routes. "
}
]
},
{
"$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
"id": "piccadilly",
"name": "Piccadilly",
"modeName": "tube",
"lineStatuses": [
{
"$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
"lineId": "piccadilly",
"statusSeverityDescription": "Severe Delays",
"reason": "Piccadilly Line: Severe delays westbound and MINOR DELAYS eastbound due to an earlier fire alert at Acton Town. Tickets are being accepted on London Buses. "
}
]
}
]