I'm running this on a Particle Photon, but I think the issue is just me failing at C++. I'm trying to retrieve some JSON data from a HTTP API, convert it to JSON, and then extract a few values for display on the screen. I convert the String to a char array for the JSON parser, but it seems to only grab the first 10 characters of the String (unless I manually set length to a number shorter than 10), and then of course, the JSON parser fails.

Here's the relevant bit of code:

int str_len = response.body.length() + 1; 
tft.print("Length: "); tft.println(str_len);
char char_array[str_len];
response.body.toCharArray(char_array, str_len);

response.body is a big JSON string, but char_array is always just the first 10 characters of it: {"metadata

Another thing I noticed just now that might be related, str_len is usually reported to be ~500 characters, but when I hit the same URL in a browser it ends up more like ~1500 characters. Neither are pretty-printed, so I'm not sure what the discrepancy is.

Here's my complete code, except I removed the base64'd username and password from the Authorization header:

#include "HttpClient/HttpClient.h"
#include "SparkJson/SparkJson.h"
#include "Adafruit_mfGFX/Adafruit_mfGFX.h"
#include "Adafruit_SSD1351_Photon/Adafruit_SSD1351_Photon.h"

// OLED - You can use any (4 or) 5 pins
#define sclk A3
#define mosi A5
#define dc   D7
#define cs   A2
#define rst  D5

// Color definitions
#define BLACK           0x0000
#define BLUE            0x001F
#define RED             0xF800
#define GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0  
#define WHITE           0xFFFF

// Option 1: Hardware SPI - uses some analog pins, but much faster
Adafruit_SSD1351 tft = Adafruit_SSD1351(cs, dc, rst);

// Option 2: Software SPI - use any pins but a little slower
//Adafruit_SSD1351 tft = Adafruit_SSD1351(cs, dc, mosi, sclk, rst);  

HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    //  { "Content-Type", "application/json" },
    { "Authorization", "Basic <snip>" }, // base64encode(username:password)
    { "Accept" , "application/json" },
    { NULL, NULL } // NOTE: Always terminate headers will NULL

http_request_t request;
http_response_t response;

void setup(void) {



    // http://twcservice.mybluemix.net/api/weather/v2/observations/current?units=e&geocode=40.0388067%2C-84.3428471&language=en-US
    request.hostname = "twcservice.mybluemix.net";
    request.port = 80;
    request.path = "/api/weather/v2/observations/current?units=e&geocode=40.0388067%2C-84.3428471&language=en-US";
    //request.body = "{\"key\":\"value\"}";
    http.get(request, response, headers);

    tft.print("Status: ");

    //tft.println("Response Body: ");

    int str_len = response.body.length() + 1; 
    tft.print("Length: "); tft.println(str_len);
    char char_array[str_len];
    response.body.toCharArray(char_array, str_len);

    DynamicJsonBuffer jsonBuffer;
    JsonObject& root = jsonBuffer.parseObject(char_array);

    if (root.success()) {
        tft.print("Current Temperature: "); 
        const char* temp = root["observation"]["imperial"]["temp"];
    } else {
        tft.println("parseObject() failed");

        uint32_t freemem = System.freeMemory();
        tft.print("Free memory: "); 



void loop() {


The actual printout on the screen is smething like

Status: 200
Length: 501
parseObject() failed
Free memory: 59k

Finally, here's an example of the JSON response I'm trying to parse:


Any idea what's going on?

  • Probably something specific to the Particle Photon. I can convert that string to and from String/char array quite happily on a Mega2560.
    – Majenko
    Apr 10, 2016 at 20:46
  • I tried it with the string hard-coded just now and that worked as expected. So maybe there's something funky with the HttpClient? Apr 10, 2016 at 21:14

1 Answer 1


It turns out that the HttpClient library has a fixed response length limit of 1024 bytes and whatever it was doing after it hit that limit is what caused all of the trouble.

Forking the library and increasing the limit as recommended at https://community.particle.io/t/issues-with-string-char-array-length-json-parsing/21980 solved the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.