Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A ArduinoJson
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 24
    • Issues 24
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Benoît Blanchon
  • ArduinoJson
  • Issues
  • #969
Closed
Open
Issue created Apr 20, 2019 by Administrator@rootContributor

Empty JSON doc most of the time

Created by: jonmacd

I'm trying to parse some JSON data. I've been successful using other sets of data. But something seems to be going wrong with this particular one. It appears to work intermittently. From time to time it does successfully print the correct piece of data. None of the GET requests themselves are failing. They're all passing with code 200.

I'm using a NodeMCU (ESP8266) with the latest version of PlatformIO and the ArduinoJson library. I used arduinojson.org/v6/assistant to calculate the dynamic document capacity.

Code:

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

const char* ssid     = "xxxxx";
const char* password = "xxxxx";

// NHL URL
const char* host = "statsapi.web.nhl.com";
const char* url = "/api/v1/schedule?teamId=10";

int requestNum = 1;

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

  Serial.println();
  Serial.println();
  Serial.print("Connecting to Wifi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.print("Netmask: ");
  Serial.println(WiFi.subnetMask());
  Serial.print("Gateway: ");
  Serial.println(WiFi.gatewayIP());
}

void loop() {
  Serial.print("GET request ");
  Serial.print(requestNum);
  Serial.println(":");
  requestNum++;
  Serial.println("-------------------------------------");
  WiFiClient client;
  if (!client.connect(host, 80)) {
    Serial.println(F("Connection failed"));
    return;
  }
  Serial.println(F("Connected!"));
  client.println(String("GET ") + url + " HTTP/1.1");
  client.print("Host: ");
  client.println(host);
  client.println("User-Agent: ESP");
  client.println();
  delay(200);
  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;
  }
  Serial.print("Get request status: ");
  Serial.println(status);

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

  // Allocate the JSON document
  // Use arduinojson.org/v6/assistant to compute the capacity.
  const size_t capacity = 2*JSON_ARRAY_SIZE(0) + 2*JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + 7*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(7) + JSON_OBJECT_SIZE(8) + JSON_OBJECT_SIZE(9) + 1707;
  DynamicJsonDocument doc(capacity);

  // Parse JSON object
  DeserializationError error = deserializeJson(doc, client);
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
  }

  // Print data
  const char* copyright = doc["copyright"];
  Serial.println(copyright);
  Serial.println("Finished printing data.");
  Serial.println("Waiting a bit to get data again!");
  Serial.println("-------------------------------------");

  // Disconnect
  client.stop();
  delay(10000);
}

And here's a serial monitor output example of the code above to show you what I mean by inconsistant. Sometimes it will correctly grab the copyright item and sometimes it's empty. If it is successful it will always work the second time around as well.

Serial Monitor Example:

WiFi connected
IP address: 
192.168.1.61
Netmask: 255.255.255.0
Gateway: 192.168.1.1
GET request 1:
-------------------------------------
Connected!
Get request status: HTTP/1.1 200 OK
NHL and the NHL Shield are registered trademarks of the National Hockey League. NHL and NHL team marks are the property of the NHL and its teams. © NHL 2019. All Rights Reserved.
Finished printing data.
Waiting a bit to get data again!
-------------------------------------
GET request 2:
-------------------------------------
Connected!
Get request status: HTTP/1.1 200 OK
NHL and the NHL Shield are registered trademarks of the National Hockey League. NHL and NHL team marks are the property of the NHL and its teams. © NHL 2019. All Rights Reserved.
Finished printing data.
Waiting a bit to get data again!
-------------------------------------
GET request 3:
-------------------------------------
Connected!
Get request status: HTTP/1.1 200 OK

Finished printing data.
Waiting a bit to get data again!
-------------------------------------
GET request 4:
-------------------------------------
Connected!
Get request status: HTTP/1.1 200 OK

Finished printing data.
Waiting a bit to get data again!
-------------------------------------
GET request 5:
-------------------------------------
Connected!
Get request status: HTTP/1.1 200 OK

Finished printing data.
Waiting a bit to get data again!
-------------------------------------
GET request 6:
-------------------------------------
Connected!
Get request status: HTTP/1.1 200 OK

Finished printing data.
Waiting a bit to get data again!
-------------------------------------
GET request 7:
-------------------------------------
Connected!
Get request status: HTTP/1.1 200 OK
NHL and the NHL Shield are registered trademarks of the National Hockey League. NHL and NHL team marks are the property of the NHL and its teams. © NHL 2019. All Rights Reserved.
Finished printing data.
Waiting a bit to get data again!
-------------------------------------
GET request 8:
-------------------------------------
Connected!
Get request status: HTTP/1.1 200 OK
NHL and the NHL Shield are registered trademarks of the National Hockey League. NHL and NHL team marks are the property of the NHL and its teams. © NHL 2019. All Rights Reserved.
Finished printing data.
Waiting a bit to get data again!
-------------------------------------
GET request 9:
-------------------------------------
Connected!
Get request status: HTTP/1.1 200 OK

Finished printing data.
Waiting a bit to get data again!
-------------------------------------
GET request 10:
-------------------------------------
Connected!
Get request status: HTTP/1.1 200 OK

Finished printing data.
Waiting a bit to get data again!
-------------------------------------
GET request 11:
-------------------------------------
Connected!
Get request status: HTTP/1.1 200 OK

Finished printing data.
Waiting a bit to get data again!
-------------------------------------
GET request 12:
-------------------------------------
Connected!
Get request status: HTTP/1.1 200 OK

Finished printing data.
Waiting a bit to get data again!
-------------------------------------
GET request 13:
-------------------------------------
Connected!
Get request status: HTTP/1.1 200 OK
NHL and the NHL Shield are registered trademarks of the National Hockey League. NHL and NHL team marks are the property of the NHL and its teams. © NHL 2019. All Rights Reserved.
Finished printing data.
Waiting a bit to get data again!
-------------------------------------
GET request 14:
-------------------------------------
Connected!
Get request status: HTTP/1.1 200 OK
NHL and the NHL Shield are registered trademarks of the National Hockey League. NHL and NHL team marks are the property of the NHL and its teams. © NHL 2019. All Rights Reserved.
Finished printing data.
Waiting a bit to get data again!
-------------------------------------
GET request 15:
-------------------------------------
Connected!
Get request status: HTTP/1.1 200 OK

Finished printing data.
Waiting a bit to get data again!
-------------------------------------
Assignee
Assign to
Time tracking