Hi there!
I was looking for a way to extract some text from json. My json comes from a REST transport API for transport situation exchange. A simplified and reduced example json result from a REST call is at the end of this post. What I am hoping for is someone that can give a few hints how to achieve the following examples (I did it already in a few lines of python but struggle to reproduce in templates):
- extract the “Description” when a “LineRef” nested inside “Affects” matches “SKY:Line:3” in the example json
- extract the “Description” when a “StopPointRef” nested inside “Affects” matches “NSR:Quay:53203” in the example json
- “ParticipantRef” also has to match my choice of “SKY” as there are others in the full dataset. So only deviations when ParticipantRef = SKY.
The result is going to be a REST sensor that displays deviations in service on a line or stop.
I am hoping for some help in this forum, thanks.
PS: If especially interested: the REST endpoint is documented here: https://developer.entur.org/pages-real-time-intro, look for SIRI Lite.
{% set value_json = {
"Siri": {
"@version": "2.0",
"@xmlns": "http://www.siri.org.uk/siri",
"@xmlns:ns2": "http://www.ifopt.org.uk/acsb",
"@xmlns:ns3": "http://www.ifopt.org.uk/ifopt",
"@xmlns:ns4": "http://datex2.eu/schema/2_0RC1/2_0",
"ServiceDelivery": {
"MoreData": "false",
"ProducerRef": "ENT",
"RequestMessageRef": "0193d38b-cb1d-76c6-9e52-c5c1147f9741",
"ResponseTimestamp": "2024-12-18T16:29:41.307902112+01:00",
"SituationExchangeDelivery": {
"@version": "2.0",
"ResponseTimestamp": "2024-12-18T16:29:41.30793295+01:00",
"Situations": {
"PtSituationElement": [
{
"Affects": {
"Networks": {
"AffectedNetwork": [
{
"AffectedLine": {
"LineRef": "SKY:Line:3",
"Routes": {
"AffectedRoute": {
"StopPoints": {
"AffectedOnly": "true",
"AffectedStopPoint": [
{
"StopPointRef": "NSR:Quay:53203"
}
]
}
}
}
},
"NetworkRef": "SKY:Network:1"
},
{
"AffectedLine": {
"LineRef": "SKY:Line:4",
"Routes": {
"AffectedRoute": {
"StopPoints": {
"AffectedOnly": "true",
"AffectedStopPoint": [
{
"StopPointRef": "NSR:Quay:53203"
}
]
}
}
}
},
"NetworkRef": "SKY:Network:1"
},
{
"AffectedLine": {
"LineRef": "SKY:Line:27",
"Routes": {
"AffectedRoute": {
"StopPoints": {
"AffectedOnly": "true",
"AffectedStopPoint": [
{
"StopPointRef": "NSR:Quay:53908"
},
{
"StopPointRef": "NSR:Quay:50177"
}
]
}
}
}
},
"NetworkRef": "SKY:Network:1"
},
{
"AffectedLine": {
"LineRef": "SKY:Line:300",
"Routes": {
"AffectedRoute": {
"StopPoints": {
"AffectedOnly": "true",
"AffectedStopPoint": [
{
"StopPointRef": "NSR:Quay:53030"
},
{
"StopPointRef": "NSR:Quay:53236"
},
{
"StopPointRef": "NSR:Quay:53245"
}
]
}
}
}
},
"NetworkRef": "SKY:Network:1"
}
]
}
},
"CreationTime": "2024-12-18T13:58:38.15+01:00",
"Description": "Glaskar- og Selviktunnelen er varsla stengd kl. 00.01–05.30 i retning mot Åsane/Knarvik på grunn av vedlikehaldsarbeid. Omkøyring via Ervikvegen kan føre til forseinkingar. Haldeplass Eidsvåg E39 blir ikkje brukt medan vegen er stengt. Bruk haldeplass Eidsvåg i Ervikveien.Linje 27: Haldeplass Tertneskrysset E39 blir ikkje brukt medan vegen er stengd. Bruk haldeplass Tertneskrysset.",
"ParticipantRef": "SKY",
"Progress": "open",
"ReportType": "general",
"Severity": "normal",
"SituationNumber": "SKY:SituationNumber:TX1197774",
"Source": {
"SourceType": "directReport"
},
"Summary": "Glaskar- og Selviktunnelen varsla stengd frå kl. 00.01",
"UndefinedReason": null,
"ValidityPeriod": {
"EndTime": "2024-12-19T05:30:00+01:00",
"StartTime": "2024-12-18T23:45:00+01:00"
}
}
]
}
}
}
}
} %}