Hello,
I have a problem parsing a JSON. I’m getting an InvalidInput error. In the template editor, the JSON looks good and is valid. All keys in the template editor have double quotes (" "), but in the ESP log, they appear with single quotes (’ '), making the JSON invalid.
"Does anyone have an idea what is going wrong here?
I use the following code to parse json:
text_sensor:
- platform: homeassistant
id: abfahrten
entity_id: sensor.berlin_departures
attribute: next_departures
on_value:
then:
- lambda: |-
ESP_LOGD("abfahrten_parse", "Raw abfahrten JSON: %s", id(abfahrten).state.c_str());
// Deserialize JSON forecast data
DynamicJsonDocument doc(4096);
auto err = deserializeJson(doc, id(abfahrten).state);
Result in log:
[
{
'delayArrival':0,
'delayDeparture':0,
'destination':'Rotenburg(Wümme)',
'isCancelled':0,
'messages':{
'delay':[
],
'qos':[
]
},
'missingRealtime':False,
'platform':'6',
'scheduledArrival':'21:37',
'scheduledDeparture':'21:38',
'scheduledPlatform':'6',
'train':'RE4',
'trainClasses':[
'D'
],
'trainNumber':'82239'
},
{
'delayArrival':0,
'delayDeparture':0,
'destination':'Hamburg-Harburg',
'isCancelled':0,
'messages':{
'delay':[
],
'qos':[
]
},
'missingRealtime':False,
'platform':'1',
'scheduledArrival':'21:58',
'scheduledDeparture':'21:59',
'scheduledPlatform':'1',
'train':'RB41',
'trainClasses':[
'D'
],
'trainNumber':'81038'
}
]
Template-Editor:
{{ state_attr("sensor.berlin_departures", 'next_departures')}}
Result:
{
"delayArrival": 5,
"delayDeparture": null,
"destination": "Berlin",
"isCancelled": 0,
"messages": {
"delay": [
{
"text": "Hohes Fahrgastaufkommen",
"timestamp": "2025-02-20T21:08:00"
}
],
"qos": [
{
"text": "Ein Wagen fehlt",
"timestamp": "2025-02-20T18:18:00"
}
]
},
"missingRealtime": false,
"platform": "11",
"scheduledArrival": "21:44",
"scheduledDeparture": null,
"scheduledPlatform": "11",
"train": "RB 38",
"trainClasses": [
"N"
],
"trainNumber": "14434"
}