Strange issue with char[] reading from SD card

Hello,
I am getting an inconsistent problem and am in search of some help. I am reading a .txt file from an SD card (to be fair, it is using the Adafruit VS1053B, but it works the same as an SD shield or breakout). As you can see in the code, I am reading the size to create a char array at the size of the file. I am simply then printing the char after the while{ }. Sometimes it prints garbage at the end, while other times it doesn’t. As you can see also I am storing the incoming char’s to a string to compare that to the char. This string looks normal while the char array does not. Any advice here? I’m scanning the boards and google as well, but only am finding info on oversized arrays or string lengths. I don’t believe that is my issue.

Code:

#include <JsonParser.h>
#include <SD.h>

File songDescriptions;
int chipSelect = 4;

//json parsing variables
JsonParser<64> parser;
JsonHashTable hashTable;

void setup(){
  Serial.begin(9600);
  Serial.println("connected to serial.");
  
  Serial.print("Free ram: ");
  Serial.println(freeRam());
  
  pinMode(chipSelect,OUTPUT);
  if(!SD.begin(chipSelect)){
    Serial.println("SD initialization failed.");
    return;  
  }
  Serial.println("SD intialized");
  
  /***Open the file from the sd card***/
  songDescriptions = SD.open("songs.txt");
  if(songDescriptions){
    Serial.println("file successfully opened.");
  } else{
    Serial.println("file didn't open.");
  }
  /************************************/
  
  Serial.print("Free ram: ");
  Serial.println(freeRam());
  
  Serial.println(songDescriptions.size());
  char json[songDescriptions.size()];
  
  Serial.print("Free ram: ");
  Serial.println(freeRam());
  
  /***Read the file and store it***/
  String s = "";
  int i = 0;
  char inputChar = songDescriptions.read();
  while(inputChar != EOF){
    //Serial.println(inputChar);
    json[i] = inputChar;
    s += inputChar;
    i++;
    inputChar = songDescriptions.read();  
  }
  //songDescriptions.close();
  /**************************************/
  Serial.println(i);
  Serial.println(sizeof(json));
  Serial.println(json);
  Serial.println(s);

}

void loop(){

}

And this is what comes out:

connected to serial.
Free ram: 1002
SD intialized
file successfully opened.
Free ram: 971
269
Free ram: 971
269
269
{
“songs”:[
{
“artist”: “the beatles”,
“song”: “love me do”,
“energy”: “low”,
“bpm”: 148,
“filename”: “beatles0.mp3”
},
{
“artist”: “smashing pumpkins”,
“song”: “zero”,
“energy”: “high”,
“bpm”: 127,
“filename”: “smash0.mp3”
}
]
}
ÿ


{
“songs”:[
{
“artist”: “the beatles”,
“song”: “love me do”,
“energy”: “low”,
“bpm”: 148,
“filename”: “beatles0.mp3”
},
{
“artist”: “smashing pumpkins”,
“song”: “zero”,
“energy”: “high”,
“bpm”: 127,
“filename”: “smash0.mp3”
}
]
}

Sometimes that " ÿ " is a different character, and sometimes there are more of them although right now each time I run it I am only seeing a single " ÿ "

Thank you for any help! I’ll post an update if I discover an answer.

You need to make your json array one bigger and null terminate it.

Thanks for the speedy response Bill!

so this?

char json[songDescriptions.size() + 1];

What does null terminate mean?

After the while loop do this...

json = '\0';

Thanks David,

When I do that I get this:

music_and_json_data.ino: In function 'void setup()': music_and_json_data:54: error: incompatible types in assignment of 'char' to 'char [(((unsigned int)((int)songDescriptions.File::size())) + 1)]'

More like:

json[i] ='\0';

Ah, that worked!

I tried changing it to:

json[sizeof(json)] = '\0';

but using " i " worked. Thanks guys!

*edit, I guess that should have been sizeof(json)-1