Parse JSON formatted file from SD card


I’ve been in trouble to parse a JSON formatted data on SD card.

I wrote a file, ‘option.txt’ like below.

{ "option" : { "ip":"", "port":3000, "id":"01", "mac": ["0xDE", "0xAD", "0xBE", "0xEF", "0xFE", "0xED"] } }

And I wrote codes following below steps.

First, I read a data from SD card.
Second, I convert this data to String. And then, convert this String to charArray using toCharArray()
Third, I convert this charArray to aJsonObject using aJson.parse() method.

I think that there is a problem on third step. but I couldn’t find the cause.

Is there anyone who has experienced similar case?

#include <aJSON.h>
#include <string.h>
#include <SdFat.h>

SdFat sd;
SdFile myFile;

#define SD_CS 4  //CS pin
#define SD_SS 10 //SS pin, 10 on Uno, 53 on Mega

// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))

void setup()
  while (!Serial) {}  // wait for Leonardo

boolean setSD(int _cs)
  if (!sd.begin(_cs, SPI_HALF_SPEED)) sd.initErrorHalt();

  // re-open the file for reading:
  if (!"option.txt", O_READ)) {
    sd.errorHalt("opening test.txt for read failed");
  String temp;
  char data;
  // make data to String;
  while ((data = >= 0) 
   temp = temp + data;
  // close the file:
  //make String to char array;
  char option[temp.length()];
  temp.toCharArray(option, temp.length()+1);
  //make char array to aJson Object
   aJsonObject* root = aJson.parse(option);

/* Process message { "option" : { "ip":"", "port":3000, "id":"01", "mac": ["0xDE", "0xAD", "0xBE", "0xEF", "0xFE", "0xED"] } }*/
void processMessage(aJsonObject *msg)
  aJsonObject *option = aJson.getObjectItem(msg, "option");
  char *json_String = aJson.print(msg);
  if (!option) {
    Serial.println("no option data");
    Serial.println("option available");

  const static char* options[] = { "ip", "port", "id", "mac" };
  const static int option_n = 4;
  for (int i = 0; i < option_n; i++) {
    char* value;
    aJsonObject *optionVal = aJson.getObjectItem(option, options[i]);
    if (!optionVal){ 
      continue; // Value not provided, ok.
      if(options[i] != "mac")
        value = aJson.print(optionVal);
        Serial.print(" : ");
        unsigned char numMAC = aJson.getArraySize( optionVal );
        Serial.print(" : ");
        for (byte i = 0; i < numMAC; i++)
            aJsonObject *item = aJson.getArrayItem( optionVal, i );
            if(i+1 < numMAC)
  char option[temp.length()];
  temp.toCharArray(option, temp.length()+1);

Suppose that temp contains 14 characters. You are allocating space, then, for 14 characters, and then telling the toCharArray() to write up to 15 characters in the array. You have the +1 in the wrong place.

I think that there is a problem on third step. but I couldn't find the cause.

What do you think the problem is? What have you done to verify that steps one and two are successful?

I also doubt the third step.

But, the toCharArray() method omit the last character

as I removed last '+1' at the method "toCharArray()".

So, I should add '+1' on that argument as buffer length.

Recently, I found that this code works fine without SdFat library.

I think there is some bugs between SdFat and aJson library.

Is there anyone who know clean SD card library which are not occurred problem with aJson library?