0

For a project I've to parse a big json file. Unfortunately it does not work as expected.

Here is the json file:

{
  "data1": "110110101101101110100001011001101010000001111111100011110000010100101011001001101000100110010100001000011111000010101100001100100101100101110111010110011001001110111000011011000100101010101111010011011000110000011010110010110011100001010100010100011100110000110011101101001001010000100100011101010111110010100000010001110100110111010100010010001110001100010001011101100000111000101101100001011100000101011101010100101000101101101010001110111010101010011101111011011011000100001100001101011011101100101100010110011001010110011111000101100000011010111010100001001001000101110011101010101111111100100100010101110001110011011001010011100111101111111000000100100011110001000110011101001100110000101110110000011111100110100000001100100100000101111100011010101011111110010011000101001011101011000001011110001101100011100111010011000110100011001111011100100010110011100011100010001011111110110111100100100011010111001000111111010111111110100111100010000010000111010000110111000101111011000010001100110111101011110110100111101110100101011111110000001111111100111011111111010111001101110001101110100000110000011111101010100001110011010101100001111010011101110010100010110110110101010100100010100100001000110100101000011110111000000010100100111110101111110011001101101111010010010101111000111100000010100001000101100001010111011001101100011111001100110011111111101100110000010110001010111101110100001100100101100111010001110001001010001100011110011111011111100100111101110000011000111001011111101100011100101001100111001100111001110100111000011101001001011000111011101111001111100000011010010001011111111010100011001001110100000000111011011110101001001011111011000000111000101010010111011000101010101010110011000010000100001110100100101001011000101001100011010100010111010101001111101001111111011101011110011010010011111011000111001100110011111011111100001100101000001111101000100011111011100110101110000111001111011000111000000011000101000000101001101000010110110110111101110100010111100000010111001001111101010100001101111100110010110011010001111100000110000111100011111100101110100101000011010110010010001111111110100010011111101000101000100110110101001000110100111011101001011001000100000010111100001001000111100111100110111000110111000111101101110111100110010001010011000010010001000001100111110101101001000111011010010110001101010010001110100110111100101111100110000110000011001001000100111101101000001111110010001101010111010110001110100110100111011010001001001100011001001111011101011011100001101101010000000101010011011000010111100100011000111111110100100111111100001110101101010001100100111000100000000010100010100000010100111000010110100111000110010010100010011111101100111001000111100101011010101000101100011100010011000101010001001011000110100110111111111001101001010000101000110011001101100101100111011101011000001000110010101101000000000100011010010111111110010101101000011010000011110101111000101101011111011000011110101110111000001000110000010001000011111011000101011111001001000111001100111011101110011101100011000101000100000011001101100100101111010100111110101001011111100001010001101100011101101011011000001010011101011001110101011011011001111110011001101010011100010001010100110010010111111111111001000101111101101101101010010100001111100111011001110001110111001110110000111000011100000101001101010111111001001010100100101101001111110000001111110010011101110000101011110110010011110010001000100111001100001011000010110101010000010011101001000011011101001110100001101100011110100000011001010001011000011000101101011111101111001100111010100111110010011100110001001010011101001101000101010111100001110011001000101001100000110111101010001101001101010110011111000000100001111101010110111100011000010111010101010100001110011100111001000001010000000000000000001000101011110001110010001010100100101010010010001001011010001011100011111100111010001100010100111110110001110000000101001101100000110000000110101010001111101011111100101100010000010111000000001010110000010011110111100001011000010110000010100111111110001101011000101010000000101111101110101110000101110111011001101101101111111000001101000011010101101101010001101001010001000101011111111010101111110100010101000101111111110001001010101010110011011101000001001111101101100000001110000100111100001000000100100100100101010111001000000101111000010111011110000011010110001010000111001100001010100011001001001110001111000100100101011010100010100100100011011010011001001111111001100000011110000001111100001010011010001011000101010111010001110000110010001011101000100111110010011100001110111011110001110110001101010101000110001110101101000001000111011110000110100101111011001011101100100001000010011101100111001110010110111101000001111100111011110101011110011111001001111000011010000011000101010011101110110101000000110001110010110100100110111000",
  "data2": "011011010000001110110011101010011101101001011001000001010001010010000111110101001010000001100011111011100011110101001010110010101110001110010001001110110011010011110011000111010010100110001110100110110011110011100110101100011100100101001111100011010111010101101000100100010010001101000101000000001111101101011101110100110110110010000001101000011010101100100010100100111001010010000001111001011111010101000110101011001001111001000100011011001010111001011001100010101000111101011101111011010111100111010000110100000111100110111110010101000111001011111000100011101111001110010010111100010001000100010100000110011100010000001011100100001010000010000000110001111000100010110110010001110100101100110001001101111011111101011110101000010101000100101011001110111110001100000001001101101001100100100110001000010001100111111001000101101100011110010111000101111100100101000110001101100000100111110100001100101101010010111000101100110001100110001001100001111111001111010001010001101000101001000110101101111101011000010001111001001100100010100010010100101000000011101011000111001011001011001111000001110111110001010010011100111010111011000011010001101000111011011010111010010000001111100110101001100010110011101010111010110011010101100000101010110011000110010000000101010101010010001110100011000110100010111001111001100001100101000001001010101011001001000001001010010101010101100001011011000111111011010111001000111101100101100100011100001010111111101100000110110110101101110010000011001010010101101010100011011110011110000010001110011000111101000100010010010110010110011101100101100000010111001011010011010100000111010110110000100010111100110011111001110100001000000110111010010011111001111110110010011011100000101000001011110110110101010110111101001110000101001011000110111110011010101100001110001110010001000110000010001100010111010010001110101101101100101000111001101111111100000011010110111101010000001011010100001011100101111111100100110100100011000000010101000101110100101011111001101110100011100100001000101011011100100000100011101000011100101000100111111011000000011100001101100011110101110111100101101110110110011100111111001101000010001011000011111100011101111111111101011101110101111100101101001111101001011101001000010100101000011101001110101001100100010101000110100000100011101010000110011010101011011111011011001101000110010110111101000110101011110000001100111010011010110010111110101111111101101111000000001001001011000000110011111100111111001000101101001000000100001110001010100101010010111111000110000110100011001010100000000110101000000100111010000111001111100010110001010110001001001011111001010110000101100110010111011001100000010101001010100110100010101001010101110001110001000001000001110011100101100101101110100100000101011100100111100101110101011001010011000101000110100100001010101001011000010001110110001000001011100100010001010010010011110001111110011101001110101000110010110001111001010101000100000100101011001110101100100101000001001000101100010101011011010000111111110010111000111001001111011100100111000111100110001110101010100101100101100010000100010001010111000100110100100101010010000011010010100010111110011111011000100011000111000001011000001110001110011110001101111001000001100001011001101000011100110111100000011011100110110100110011110101101011001001110111001001011101110001100110001100001011101010100110101110100110001101111011010011101001011111011101101101010000111101110101011000000111010000100010111000011110110100011110010010101111110110111000011011110100111100100111010001011000010000101010010010010110100000101110111111110001110000110101100110011100100110110110000011111010010111110111001000101010110010110101001010000110001100001100111100110000000000111110110100110110000000001110111100100111111100010011101101101111000011010111010101000011101010001010010011011010010011110100010101001010010110111001000110101011000001110011011110000110110100000111000101000000010101011001110111011000010101110000011100010001001111101001111101001011000110111001001011011110110010101100011000111110110001100101010010001001111010100111001100011011001000010101010111000010011001011011011011011011111100010010110011001101111010110011110110011011010011000101001010101011000000010001100101101010011011001100101000000011011111000010110011111110100010111101010111001111000110110011011101101101100111010010110011010110101011110101101010101100110101011011110110111000011000011011100100000001101001111111111101100010001110110011010101101010000000111100111001100011101110101101000111010100101000111101001111100010111010110010110001101001011011101000111101110001100000111100010101011011111011100110100101100001100100101000000001100010101001100010000111001010000111001000100000011101111010101100011011010110101110001111000001000011111010001001100100100111011100100001100010001001010101011011101101",
  "data3": "736",
  "data4": "266",
  "data5": "3211",
  "data6": "0"
}

I was able to successfully implement the whole thing on an ESP8266 with this code:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>

// WiFi Parameters
const char* ssid = "SSID";
const char* password = "PASSWORD";

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting...");
  }
}

void loop() {
  // Check WiFi Status
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;  //Object of class HTTPClient
    http.begin("http://jsonplaceholder.typicode.com/users/1");
    int httpCode = http.GET();
    //Check the returning code                                                                  
    if (httpCode > 0) {
      // Parsing
      const size_t bufferSize = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(8) + 370;
      DynamicJsonBuffer jsonBuffer(bufferSize);
      JsonObject& root = jsonBuffer.parseObject(http.getString());

      const char* data1 = root["data1"]; 
      const char* data2 = root["data2"]; 
      const char* data3 = root["data3"]; 
      const char* data4 = root["data4"]; 
      const char* data5 = root["data5"];
      const char* data6 = root["data6"];  

      Serial.print("data1:");
      Serial.println(data1);
      Serial.print("data2:"); 
      Serial.println(data2);
      Serial.print("data3:"); 
      Serial.println(data3);
      Serial.print("data4:"); 
      Serial.println(data4);
      Serial.print("data5:"); 
      Serial.println(data5);
      Serial.print("data6:"); 
      Serial.println(data6);
    }
    http.end();   //Close connection
  }
  delay(60000);
}

The ESP Code works perfect for me, but for my application I need an microcontroller with ethernet. So I bought an enc28j60 and a W5500 Board to migrate the ESP Code to the Arduino UNO. The Code result is this:

#include <ArduinoJson.h>
#include <Ethernet.h>
#include <SPI.h>

void setup() {
  // Initialize Serial port
  Serial.begin(9600);
  while (!Serial) continue;

  // Initialize Ethernet library
  byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  if (!Ethernet.begin(mac)) {
    Serial.println(F("Failed to configure Ethernet"));
    return;
  }
  delay(1000);

  Serial.println(F("Connecting..."));

  // Connect to HTTP server
  EthernetClient client;
  client.setTimeout(10000);
  if (!client.connect("192.168.178.5", 80)) {
    Serial.println(F("Connection failed"));
    return;
  }

  Serial.println(F("Connected!"));

  // Send HTTP request
  client.println(F("GET /test.json HTTP/1.0"));
  client.println(F("Host: arduinojson.org"));
  client.println(F("Connection: close"));
  if (client.println() == 0) {
    Serial.println(F("Failed to send request"));
    return;
  }

  // Check HTTP status
  char status[32] = {0};
  client.readBytesUntil('\r', status, sizeof(status));
  if (strcmp(status, "HTTP/1.1 200 OK") != 0) {
    Serial.print(F("Unexpected response: "));
    Serial.println(status);
    return;
  }

  // Skip HTTP headers
  char endOfHeaders[] = "\r\n\r\n";
  if (!client.find(endOfHeaders)) {
    Serial.println(F("Invalid response"));
    return;
  }

  // Allocate JsonBuffer
  // Use arduinojson.org/assistant to compute the capacity.
  const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
  DynamicJsonBuffer jsonBuffer(capacity);

  // Parse JSON object
  JsonObject& root = jsonBuffer.parseObject(client);
  if (!root.success()) {
    Serial.println(F("Parsing failed!"));
    return;
  }

  // Extract values
  Serial.println(F("Response:"));
  Serial.println(root["data1"].as<char*>());
  Serial.println(root["data2"].as<char*>());
  Serial.println(root["data3"].as<char*>());
  Serial.println(root["data4"].as<char*>());
  Serial.println(root["data5"].as<char*>());
  Serial.println(root["data6"].as<char*>());

  // Disconnect
  client.stop();
}

void loop() {

}

Every time a run the sketch I get the following error:

"Parsing failed!"

So I got to the following page:

https://arduinojson.org/v5/faq/why-parsing-fails/

I've tested the sketch with different buffer sizes, without success?

Is it possible that the memory simply is too small? Or did I miss something?

  • you can use W5500 or enc28j60 with esp8266. use MemoryFree library to print free memory size at runtime – Juraj Apr 5 at 11:22
  • Do you have an example or a sketch for me? I did not make it work. – Philipp Apr 5 at 11:24
  • use esp8266 package 2.4.2 and delete the bundled Ethernet library – Juraj Apr 5 at 11:24
  • try an esp32, they have phy built in, and a lot more ram to boot... – dandavis Apr 5 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.