Hello.
I have a problem with parsing a very long JSON answer that I want to analyze. The answer is a list of all of a certain user's SoundCloud tracks. I am using a Wemos D1 mini and the famous JSON library by bblanchon. I heavily rely on his own example sketches and when I use them with API request that only have short answers everything works fine. As soon as I try to parse the long list of tracks though, the sketch tells me "parsing failed".
Is this a common problem with longish JSON answers? Am I running out of memory? How much memory does the ESP8266 have? Is there a chance to store that huge string away somewhere else intermediately?
I used the tool to calculate the memory needed https://bblanchon.github.io/ArduinoJson/assistant/ . The API request and its very long answer can be seen here: http://api.soundcloud.com/users/229446377/tracks?client_id=JlZIsxg2hY5WnBgtn3jfS0UYCl0K8DOg . And my complete sketch that throws the error is here: Arduino Cloud .
This is the bit of code that encounters the error:
// Parse the JSON from the input string and extract the interesting values
bool readResponseContent(struct UserData* userData) {
// Compute optimal size of the JSON buffer according to what we need to parse.
// See https://bblanchon.github.io/ArduinoJson/assistant/
const size_t BUFFER_SIZE =
JSON_ARRAY_SIZE(18) // 18 Objects in Array bc 18 tracks
+ 18*JSON_OBJECT_SIZE(8) // 8 items in user object
+ 18*JSON_OBJECT_SIZE(48) // 48 items in every object
+ MAX_CONTENT_SIZE; // additional space for strings as given at the top
// Allocate a temporary memory pool
DynamicJsonBuffer jsonBuffer(BUFFER_SIZE);
Serial.println("JSON Buffer is: ");
Serial.println(BUFFER_SIZE);
JsonArray& root = jsonBuffer.parseArray(client); //client response being parsed into root
JsonArray& root_ = root;
JsonObject& root_0 = root_[0];
int root_0_playback_count = root_0["playback_count"];
if (!root.success()) {
Serial.println("JSON parsing failed!");
return false;
}
// Here were copy the value we're interested in
userData->playCount = root_0_playback_count;
return true;
}
And here's what the serial monitor shows:
Connect to api.soundcloud.com
Connected
GET /users/229446377/tracks?client_id=JlZIsxg2hY5WnBgtn3jfS0UYCl0K8DOg
JSON Buffer is:
74357
JSON parsing failed!
Disconnect
Wait 60 seconds