Gettting Null values from JsonDocument
Created by: VishBK
This is my code:
Serial.println("Parsing");
char input[] = "{\"recenttracks\":{\"track\":[{\"artist\":{\"url\":\"https://www.last.fm/music/Daft+Punk\",\"name\":\"Daft Punk\",\"image\":[{\"size\":\"small\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png\"},{\"size\":\"medium\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png\"},{\"size\":\"large\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png\"},{\"size\":\"extralarge\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png\"}],\"mbid\":\"\"},\"mbid\":\"069536c6-45f6-489f-93ae-916ab1396cbb\",\"name\":\"Castor\",\"image\":[{\"size\":\"small\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/34s/6bf3e1cd10aad29f0f3da398f22d3a23.jpg\"},{\"size\":\"medium\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/64s/6bf3e1cd10aad29f0f3da398f22d3a23.jpg\"},{\"size\":\"large\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/174s/6bf3e1cd10aad29f0f3da398f22d3a23.jpg\"},{\"size\":\"extralarge\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/300x300/6bf3e1cd10aad29f0f3da398f22d3a23.jpg\"}],\"streamable\":\"0\",\"album\":{\"mbid\":\"\",\"#text\":\"TRON: Legacy - The Complete Edition (Original Motion Picture Soundtrack)\"},\"url\":\"https://www.last.fm/music/Daft+Punk/_/Castor\",\"@attr\":{\"nowplaying\":\"true\"},\"loved\":\"0\"},{\"artist\":{\"url\":\"https://www.last.fm/music/Mac+Quayle\",\"name\":\"Mac Quayle\",\"image\":[{\"size\":\"small\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png\"},{\"size\":\"medium\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png\"},{\"size\":\"large\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png\"},{\"size\":\"extralarge\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png\"}],\"mbid\":\"\"},\"date\":{\"uts\":\"1641801521\",\"#text\":\"10 Jan 2022, 07:58\"},\"mbid\":\"ac7937d9-7065-4f42-b715-b7b9195f80dc\",\"name\":\"1.1_4-hateurself.ra\",\"image\":[{\"size\":\"small\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/34s/fbe7813f8258abdeeaae93402d8a03fc.jpg\"},{\"size\":\"medium\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/64s/fbe7813f8258abdeeaae93402d8a03fc.jpg\"},{\"size\":\"large\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/174s/fbe7813f8258abdeeaae93402d8a03fc.jpg\"},{\"size\":\"extralarge\",\"#text\":\"https://lastfm.freetls.fastly.net/i/u/300x300/fbe7813f8258abdeeaae93402d8a03fc.jpg\"}],\"url\":\"https://www.last.fm/music/Mac+Quayle/_/1.1_4-hateurself.ra\",\"streamable\":\"0\",\"album\":{\"mbid\":\"44e399d5-30d5-4098-885d-a7331beb7b61\",\"#text\":\"Mr. Robot, Vol. 1 (Original Television Series Soundtrack)\"},\"loved\":\"0\"}],\"@attr\":{\"user\":\"VishBK\",\"totalPages\":\"48780\",\"page\":\"1\",\"perPage\":\"1\",\"total\":\"48780\"}}}";
DynamicJsonDocument doc(4096);
DeserializationError error = deserializeJson(doc, input);
if (error) {
Serial.print("deserializeJson() failed for LastFM: ");
Serial.println(error.c_str());
}
JsonObject parsed = doc["recenttracks"]["track"][0];
serializeJson(parsed, Serial);
and this is what parsed
prints out:
{
"artist": {
"url": "https://www.last.fm/music/Daft+Punk",
"name": "Daft Punk",
"image": [
{
"size": "small",
"#text": "https://lastfm.freetls.fastly.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png"
},
{
"size": "medium",
"#text": "https://lastfm.freetls.fastly.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png"
},
{
"size": "large",
"#text": "https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png"
},
{
"size": "extralarge",
"#text": "https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png"
}
],
"mbid": ""
},
"mbid": "069536c6-45f6-489f-93ae-916ab1396cbb",
"name": "Castor",
"image": [
{
"size": "small",
"#text": "https://lastfm.freetls.fastly.net/i/u/34s/6bf3e1cd10aad29f0f3da398f22d3a23.jpg"
},
{
"size": "medium",
"#text": "https://lastfm.freetls.fastly.net/i/u/64s/6bf3e1cd10aad29f0f3da398f22d3a23.jpg"
},
{
"size": "large",
"#text": "https://lastfm.freetls.fastly.net/i/u/174s/6bf3e1cd10aad29f0f3da398f22d3a23.jpg"
},
{
"size": "extralarge",
"#text": "https://lastfm.freetls.fastly.net/i/u/300x300/6bf3e1cd10aad29f0f3da398f22d3a23.jpg"
}
],
"streamable": "0",
"album": {
"mbid": "",
"#text": "TRON: Legacy - The Complete Edition (Original Motion Picture Soundtrack)"
},
"url": "https://www.last.fm/music/Daft+Punk/_/Castor",
"@attr": {
"nowplaying": "true"
},
"loved": "0"
}
However, I cannot access anything else in the JSON, such as parsed["image"][1]["#text"]
, it just returns null. I've checked the size with the assistant, and even the assistant's code is not working. Any ideas what the problem is?