Parsing JSON API only works sometimes

Hey Guys,
im making my first steps in the Arduino world, so please be kind if i have some basic errors here...

I want to ask a SSL JSON API for some values, to store them in a variable and later display them on a SH1106 Display. So first i need to get the Values and this is where im struggeling on multiple mystery problems..
First problem: I get the API-Answer mostly only on first loop, if i get an Answer at all.
Second problem: The parsing into my variables only works on first loop, if it works at all.

The strange thing is that the Code works sometimes, but mostly not. I have the feeling that its somehow memory related. I dont know if i have the correct SPIFFS-Setting, tried no or 3M but doesnt get better.
The API-Response is somewhat large (8,17 kb), is this to much to get parsed stable?

Board: NodeMcu v3 with ESP-8266
Settings: NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Disabled, 4M (3M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch
API: https://api.corrently.io/core/gsi?plz=97082
Values im interested: "forecast_0_eevalue" and "forecast_0_energyprice". More maybe later, but currently the current time (0) is enough.

So would you kindly please have a look at my code?
Heavy use of Serial.print and some tests for debugging, but didnt help..
The out-commented things are for debugging, later use or replaced from the sample code my code is based on.

#include <ESP8266WiFi.h>
#include <ESP8266Ping.h>
//#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>

// WiFi
const char* WIFI_SSID = "xxx";
const char* WIFI_PWD = "xxx";

// API-Server
const char* host = "api.corrently.io";
const int httpsPort = 443;
const char fingerprint[] PROGMEM = "CD B8 85 5C D3 0B D8 06 C3 B9 43 9A B9 06 4C 4E 36 B9 65 AE"; //Valid til Donnerstag, 7. November 2019

// Declaring Variables
//String line;

//Test API-Response
//const String lineTest = "{\"forecast\":[{\"epochtime\":155... Snip. Sample-API-Response-here...}";


void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(WIFI_SSID);
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PWD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
// Declaring Variables
String line;
int pingResult;
  
  // Check WiFi Status
  if (WiFi.status() == WL_CONNECTED) {
    
  long rssi = WiFi.RSSI();
  Serial.print("RSSI: ");
  Serial.println(rssi);
  
  Ping.ping("corrently.io");
  int pingResult = Ping.averageTime();
  Serial.print(pingResult);
  Serial.println(" ms ping");
    
  
  // Use WiFiClientSecure class to create TLS connection
  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);

  Serial.printf("Using fingerprint '%s'\n", fingerprint);
  client.setFingerprint(fingerprint);

  if (!client.connect(host, httpsPort)) {
    Serial.println("TLS connection failed");
    return;
  }


  // get the data
  String url = "/core/gsi?plz=97082";
  //Serial.println("wait 10s to relax the API");
  //delay(10000);
  
  Serial.print("requesting URL: ");
  Serial.println(url);

  // disable for use of lineTEST
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: GSIDisplayTEST\r\n" +
               "Connection: close\r\n\r\n");
               
//DEBUG!!!
Serial.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: GSIDisplayTEST\r\n" +
              "Connection: close\r\n\r\n");
              //   "Connection: open\r\n\r\n");
               
   delay(1000); //DEBUG!!!!!!!!!!!
  Serial.println("request sent");

  while (client.connected()) {
    String line = client.readStringUntil('\n');
    Serial.println(line); //DEBUG!!!!!!!!!!!!!!!!!!
    if (line == "") {
      Serial.println("empty Line");
      }
    if (line == "\r") {
      Serial.println("headers received");
      break;
      }
    /** mysteries of old code....
    while (client.available()) {
      String line = client.readStringUntil('\n');
      Serial.println(line);
      **/
    }
 String line = client.readStringUntil('\n');
 //String  line = lineTest; //TEST-debug

//check reply
  if (line.startsWith("{\"forecast\":[{\"epochtime\"")) {
    Serial.println("Request successfull!");
  } else {
    Serial.println("Request has failed");
  }

  Serial.println("reply was:");
  Serial.println("==========");
  Serial.println(line);
  Serial.println("==========");
  Serial.println("HTTPS GET done");
  //client.stop();

    //Check the returning code                                                                  
    if (line.length() > 0) {
//    if (1 > 0) {

    // Parsing
    Serial.println("parsing...");
const size_t capacity = JSON_ARRAY_SIZE(48) + 2*JSON_OBJECT_SIZE(2) + 48*JSON_OBJECT_SIZE(7) + 5600;
//const size_t capacity = JSON_ARRAY_SIZE(48) + 2*JSON_OBJECT_SIZE(2) + 48*JSON_OBJECT_SIZE(7) + 5900;
DynamicJsonBuffer jsonBuffer(capacity);

//JSON Source:
    JsonObject& root = jsonBuffer.parseObject(line);


JsonArray& forecast = root["forecast"];

JsonObject& forecast_0 = forecast[0];
long forecast_0_epochtime = forecast_0["epochtime"]; // 1551621600
int forecast_0_eevalue = forecast_0["eevalue"]; // 56
int forecast_0_ewind = forecast_0["ewind"]; // 56
int forecast_0_esolar = forecast_0["esolar"]; // 5
int forecast_0_gsi = forecast_0["gsi"]; // 56
long forecast_0_timeStamp = forecast_0["timeStamp"]; // 1551621600000
//float forecast_0_energyprice = forecast_0["energyprice"]; // "-0.0280000"
const char* forecast_0_energyprice = forecast_0["energyprice"]; // "-0.0280000"
//snipp
const char* location_city = root["location"]["city"]; // "Würzburg"

if (!root.success()) {
  Serial.println("Parsing root failed");
  //return;
}
if (!forecast.success()) {
  Serial.println("Parsing forecast failed");
  //return;
}

      // Output to serial monitor
      Serial.print("Location: ");
      Serial.println(location_city);
      //Serial.print("Vorhersage für: ");
      //Serial.println(forecast_0_epochtime);
      Serial.print("EE Value: ");
      Serial.println(forecast_0_eevalue);
      Serial.print("Energyprice: "); 
      Serial.println(forecast_0_energyprice);
      
    }
  }
  // Delay
  delay(60000);
}




/**
https://randomnerdtutorials.com/decoding-and-encoding-json-with-arduino-or-esp8266/
https://www.instructables.com/id/ESP8266-Parsing-JSON/
https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/HTTPSRequest/HTTPSRequest.ino 

#include <JsonListener.h>

*/

Have an nice day :slight_smile:

The Serial Output of 4 loops:

10:59:17.210 -> 
10:59:17.210 -> connecting to xxx
10:59:17.210 -> .
10:59:17.210 -> WiFi connected
10:59:17.210 -> IP address: 192.168.2.104
10:59:17.210 -> RSSI: -53
10:59:21.350 -> 46 ms ping
10:59:21.350 -> connecting to api.corrently.io
10:59:21.350 -> Using fingerprint 'CD B8 85 5C D3 0B D8 06 C3 B9 43 9A B9 06 4C 4E 36 B9 65 AE'
10:59:23.719 -> requesting URL: /core/gsi?plz=97082
10:59:23.821 -> GET /core/gsi?plz=97082 HTTP/1.1
10:59:23.821 -> Host: api.corrently.io
10:59:23.821 -> User-Agent: GSIDisplayTEST
10:59:23.821 -> Connection: close
10:59:23.821 -> 
10:59:24.796 -> request sent
10:59:27.141 -> HTTP/1.1 200 OK
10:59:27.141 -> Date: Sat, 16 Mar 2019 09:59:20 GMT
10:59:27.141 -> Content-Type: application/json
10:59:27.141 -> Content-Length: 8367
10:59:27.141 -> Connection: close
10:59:27.175 -> x-amzn-RequestId: 2b315ba5-47d2-11e9-93d2-8300f8df8edf
10:59:27.175 -> Access-Control-Allow-Origin: *
10:59:27.175 -> Access-Control-Allow-Headers: Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token
10:59:27.175 -> x-amz-apigw-id: WoRW0FivliAFsig=
10:59:27.175 -> Access-Control-Allow-Methods: GET,OPTIONS
10:59:27.175 -> X-Amzn-Trace-Id: Root=1-5c8cc8f8-2ace82c4b902d37c8c486e6a;Sampled=0
10:59:27.175 -> Access-Control-Max-Age: 0
10:59:27.175 -> Access-Control-Allow-Credentials: true
10:59:27.210 -> 
10:59:27.210 -> headers received
10:59:42.666 -> Request successfull!
10:59:42.666 -> reply was:
10:59:42.666 -> ==========
10:59:42.666 -> {"forecast":[{"epochtime":1552.. Snipp. Answer was good.}
10:59:43.378 -> ==========
10:59:43.378 -> HTTPS GET done
10:59:43.378 -> parsing...
10:59:43.378 -> Parsing root failed
10:59:43.378 -> Parsing forecast failed
10:59:43.378 -> Location: 
10:59:43.378 -> EE Value: 0
10:59:43.378 -> Energyprice: 
11:00:43.375 -> RSSI: -52
11:00:47.491 -> 63 ms ping
11:00:47.491 -> connecting to api.corrently.io
11:00:47.491 -> Using fingerprint 'CD B8 85 5C D3 0B D8 06 C3 B9 43 9A B9 06 4C 4E 36 B9 65 AE'
11:01:47.672 -> TLS connection failed
11:01:47.672 -> RSSI: -50
11:01:51.811 -> 36 ms ping
11:01:51.811 -> connecting to api.corrently.io
11:01:51.811 -> Using fingerprint 'CD B8 85 5C D3 0B D8 06 C3 B9 43 9A B9 06 4C 4E 36 B9 65 AE'
11:02:03.065 -> requesting URL: /core/gsi?plz=97082
11:02:03.134 -> GET /core/gsi?plz=97082 HTTP/1.1
11:02:03.134 -> Host: api.corrently.io
11:02:03.134 -> User-Agent: GSIDisplayTEST
11:02:03.134 -> Connection: close
11:02:03.134 -> 
11:02:04.151 -> request sent
11:02:08.569 -> HTTP/1.1 200 OK
11:02:08.569 -> Date: Sat, 16 Mar 2019 10:02:00 GMT
11:02:08.569 -> Content-Type: application/json
11:02:08.569 -> Content-Length: 8194
11:02:08.569 -> Connection: close
11:02:08.569 -> x-amzn-RequestId: 8a297e69-47d2-11e9-a05c-a9a24e8bb438
11:02:08.569 -> Access-Control-Allow-Origin: *
11:02:08.569 -> Access-Control-Allow-Headers: Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token
11:02:08.603 -> x-amz-apigw-id: WoRvuEFiFiAFSdQ=
11:02:08.603 -> Access-Control-Allow-Methods: GET,OPTIONS
11:02:08.603 -> X-Amzn-Trace-Id: Root=1-5c8cc997-37b0706822d1f6b87eded950;Sampled=0
11:02:08.603 -> Access-Control-Max-Age: 0
11:02:08.603 -> Access-Control-Allow-Credentials: true
11:02:08.603 -> 
11:02:08.603 -> headers received
11:02:24.048 -> Request successfull!
11:02:24.048 -> reply was:
11:02:24.048 -> ==========
11:02:24.048 -> {"forecast":[{"epochtime":1552... Snipp. Answer was good.}
11:02:24.767 -> ==========
11:02:24.767 -> HTTPS GET done
11:02:24.767 -> parsing...
11:02:24.767 -> Parsing root failed
11:02:24.767 -> Parsing forecast failed
11:02:24.767 -> Location: 
11:02:24.767 -> EE Value: 0
11:02:24.767 -> Energyprice: 
11:03:24.779 -> RSSI: -50
11:03:29.028 -> 50 ms ping
11:03:29.028 -> connecting to api.corrently.io
11:03:29.028 -> Using fingerprint 'CD B8 85 5C D3 0B D8 06 C3 B9 43 9A B9 06 4C 4E 36 B9 65 AE'

Nothing any more. I hit the reset Button.

11:06:47.663 -> connecting to xxx
11:06:48.171 -> .
11:06:48.171 -> WiFi connected
11:06:48.171 -> IP address: 192.168.2.104
11:06:48.171 -> RSSI: -57
11:06:52.270 -> 28 ms ping
11:06:52.270 -> connecting to api.corrently.io
11:06:52.270 -> Using fingerprint 'CD B8 85 5C D3 0B D8 06 C3 B9 43 9A B9 06 4C 4E 36 B9 65 AE'
11:06:56.713 -> requesting URL: /core/gsi?plz=97082
11:06:56.780 -> GET /core/gsi?plz=97082 HTTP/1.1
11:06:56.780 -> Host: api.corrently.io
11:06:56.780 -> User-Agent: GSIDisplayTEST
11:06:56.780 -> Connection: close
11:06:56.780 -> 
11:06:57.800 -> request sent
11:06:58.887 -> HTTP/1.1 200 OK
11:06:58.887 -> Date: Sat, 16 Mar 2019 10:06:54 GMT
11:06:58.887 -> Content-Type: application/json
11:06:58.922 -> Content-Length: 8194
11:06:58.922 -> Connection: close
11:06:58.922 -> x-amzn-RequestId: 39309f07-47d3-11e9-a05c-a9a24e8bb438
11:06:58.922 -> Access-Control-Allow-Origin: *
11:06:58.922 -> Access-Control-Allow-Headers: Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token
11:06:58.922 -> x-amz-apigw-id: WoSdmE0aliAFSdQ=
11:06:58.922 -> Access-Control-Allow-Methods: GET,OPTIONS
11:06:58.922 -> X-Amzn-Trace-Id: Root=1-5c8ccabd-e654c05a8b660a7cdb76a324;Sampled=0
11:06:58.956 -> Access-Control-Max-Age: 0
11:06:58.956 -> Access-Control-Allow-Credentials: true
11:06:58.956 -> 
11:06:58.956 -> headers received
11:07:14.397 -> Request successfull!
11:07:14.397 -> reply was:
11:07:14.397 -> ==========
11:07:14.397 -> {"forecast":[{"epochtime":1552... Snip. Answer was good.}
11:07:15.113 -> ==========
11:07:15.113 -> HTTPS GET done
11:07:15.113 -> parsing...
11:07:15.113 -> Parsing root failed
11:07:15.113 -> Parsing forecast failed
11:07:15.113 -> Location: 
11:07:15.113 -> EE Value: 0
11:07:15.113 -> Energyprice: