Parsing JSON - Buffer help

Hi all, I'm trying to create a little arduino monitor for Tautulli (plex-py). I'm pretty much a novice so any help would be appreciated.

I have managed to work through the example JSON code, port it for my needs and get to this point:

My problem is if I get more than 2 streams playing it won't parse the JSON. I get "parsing failed!" in the serial monitor. I know my buffer is the issue but don't have the skills to solve it.

The problem is the JSON returned from the Tautulli api changes in size and I think this is where the issue lies.

Here is the JSON with nothing playing.

The only 2 values I care about are Wan_bandwidth and Stream_count.

When something is playing it expands the "Sessions" and adds a load of crap I'm not interested in about the show. Every show that plays shows under sessions as 0,1,2 etc.

I've attached the different JSON responses.

Here is my code:

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels


// Declaration for SSD1306 display connected using software SPI:
#define OLED_MOSI  D7
#define OLED_CLK   D5
#define OLED_DC    D4
#define OLED_CS    D8
#define OLED_RESET D0
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);


const char* wifiName = "MYSSID";
const char* wifiPass = "MYPASS";

//Web Server address to read/write from 
const char *host = "https://SERVERADDRESS/api/v2?apikey=xxxAPIKEYxxx&cmd=get_activity";

void setup() {
  
  Serial.begin(115200);
  delay(10);

// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }


  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(1000); // Pause for 1 second

  // Clear the buffer
  display.clearDisplay();
  Serial.println();
  
  Serial.print("Connecting to ");
  Serial.println(wifiName);

  WiFi.begin(wifiName, wifiPass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());   //You can get IP address assigned to ESP
}

void loop() {
  HTTPClient http;    //Declare object of class HTTPClient

  Serial.print("Request Link:");
  Serial.println(host);
  
  http.begin(host);     //Specify request destination
  
  int httpCode = http.GET();            //Send the request
  String payload = http.getString();    //Get the response payload from server

  Serial.print("Response Code:"); //200 is OK
  Serial.println(httpCode);   //Print HTTP return code

  Serial.print("Returned data from Server:");
  Serial.println(payload);    //Print request response payload
  
  if(httpCode == 200)
  {
    // Allocate JsonBuffer
    // Use arduinojson.org/v5/assistant to compute the capacity.
    const size_t capacity = JSON_ARRAY_SIZE(0) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(8) + 16720;
   
    DynamicJsonBuffer jsonBuffer(capacity);
  
   // Parse JSON object
    JsonObject& root = jsonBuffer.parseObject(payload);
    if (!root.success()) {
      Serial.println(F("Parsing failed!"));
      delay(3000);
      return;
    }

    // Display data in serial monitor
    Serial.println(F("Response:"));
    Serial.println(root["response"]["data"]["wan_bandwidth"].as<char*>());
    Serial.println(root["response"]["data"]["stream_count"].as<char*>());
    
    //Display data 
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Bandwidth Used");
display.setCursor(0, 12);
display.setTextSize(2);
display.print(root["response"]["data"]["wan_bandwidth"].as<char*>());
display.println(" kbps");
display.setCursor(0, 35);
display.setTextSize(1);
display.println("No. of Streams");
display.setCursor(0, 47);
display.setTextSize(2);
display.print(root["response"]["data"]["stream_count"].as<char*>());

display.display(); 

delay(1000);
  }
  else
  {
    Serial.println("Error in response");
  }

  http.end();  //Close connection
  
  delay(5000);  //GET Data at every 5 seconds
}

If I add the output of the JSON to Assistant | ArduinoJson 5 when I have 1, 2, 3 or 4 streams playing I get different buffer sizes. None of them seem to be a one size fits all.

My question is does anyone know how to create the buffer that omits the "sessions" section?

JSON with nothing playing.txt (343 Bytes)

JSON with one show playing.txt (9.8 KB)

Have a look at How to deserialize a very large document?

Alternatively May be look in the python source code the get_activity API and create a new API entry returning only what you need