Hi there
I'm trying to retrieve a specific value from a JSON feed but am getting a NaN result when it should return 21.5.
This is the raw JSON as displayed in the Serial Monitor:
==RAW JSON==
[{"LocalObservationDateTime":"2025-08-01T12:06:00+01:00","EpochTime":1754046360,"WeatherText":"Mostly cloudy","WeatherIcon":6,"HasPrecipitation":false,"PrecipitationType":null,"IsDayTime":true,"Temperature":{"Metric":{"Value":21.5,"Unit":"C","UnitType":17},"Imperial":{"Value":71.0,"Unit":"F","UnitType":18}},"MobileLink":"http://www.accuweather.com/en/gb/saltdean/bn2-8/current-weather/2524451?lang=en-us","Link":"http://www.accuweather.com/en/gb/saltdean/bn2-8/current-weather/2524451?lang=en-us"}]
==================
My locate data function in the sketch is:
void showWeather() {
String json = getWeatherData(); //returns the ==RAW JSON== data
Serial.println("==RAW JSON==");
Serial.println(json);
Serial.println("==================");
JSONVar root = JSON.parse(json);
float temp_c = (double) root["0"]["Temperature"]["Metric"]["Value"];
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.setCursor(170, 89);
tft.print(temp_c, 0);
}
I assume I have a JSON addressing error but I've tried a few variations without luck. Where am I going wrong?
Many thanks
PaulRB
August 1, 2025, 12:09pm
2
It's helpful to format the JSON for us humans
[
{
"LocalObservationDateTime": "2025-08-01T12:06:00+01:00",
"EpochTime": 1754046360,
"WeatherText": "Mostly cloudy",
"WeatherIcon": 6,
"HasPrecipitation": false,
"PrecipitationType": null,
"IsDayTime": true,
"Temperature": {
"Metric": {
"Value": 21.5,
"Unit": "C",
"UnitType": 17
},
"Imperial": {
"Value": 71,
"Unit": "F",
"UnitType": 18
}
},
"MobileLink": "http://www.accuweather.com/en/gb/saltdean/bn2-8/current-weather/2524451?lang=en-us",
"Link": "http://www.accuweather.com/en/gb/saltdean/bn2-8/current-weather/2524451?lang=en-us"
}
]
PaulRB
August 1, 2025, 12:14pm
3
dbootle:
root["0"]
Should that zero have quote marks around it? It is an array index, not a label. This is a guess, I'm not that familiar with the Arduino JSON library.
PaulRB
August 1, 2025, 12:20pm
4
Try
void showWeather() {
String json = getWeatherData(); //returns the ==RAW JSON== data
Serial.println("==RAW JSON==");
Serial.println(json);
Serial.println("==================");
JSONVar root = JSON.parse(json);
String temp_c_str = root["0"]["Temperature"]["Metric"]["Value"];
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.setCursor(170, 89);
tft.print(temp_c_str);
}
I realise this might not give the formatting you wanted, but does it work at all?
PaulRB
August 1, 2025, 12:22pm
5
Also try
void showWeather() {
String json = getWeatherData(); //returns the ==RAW JSON== data
Serial.println("==RAW JSON==");
Serial.println(json);
Serial.println("==================");
JSONVar root = JSON.parse(json);
float temp_c = root["0"]["Temperature"]["Metric"]["Value"].toFloat();
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.setCursor(170, 89);
tft.print(temp_c, 0);
}
Thanks again PaulRB... much appreciated. I'm sure I tried this before posting but must have got something wrong
Sorry also for not formatting the JSON nicely. I WILL do better!
I use this site to check if my json string is correct:
“https://jsonviewer.stack.hu/
Look your json at site:
kenb4
August 1, 2025, 8:44pm
8
One nice forum feature: syntax highlighting for things other than "Arduino" or C++. You can choose the language in the WYSIWYG editor, which translates to having the designator after the opening triple-backtick in the Markdown: ```json in this case.
[
{
"LocalObservationDateTime": "2025-08-01T12:06:00+01:00",
"EpochTime": 1754046360,
"WeatherText": "Mostly cloudy",
"WeatherIcon": 6,
"HasPrecipitation": false,
"PrecipitationType": null,
"IsDayTime": true,
"Temperature": {
"Metric": {
"Value": 21.5,
"Unit": "C",
"UnitType": 17
},
"Imperial": {
"Value": 71,
"Unit": "F",
"UnitType": 18
}
},
"MobileLink": "http://www.accuweather.com/en/gb/saltdean/bn2-8/current-weather/2524451?lang=en-us",
"Link": "http://www.accuweather.com/en/gb/saltdean/bn2-8/current-weather/2524451?lang=en-us"
}
]
Now there are different colors for the quoted field names versus the field values that are strings; and null is colored the same as true and false (the only three "plain words" allowed in JSON)