Write on JSON file on SD card with Arduino
<p>I have an Arduino project where I need to write a JSON file on an SD card. </p>

<p>I'm using the SD library from arduino. If I write my content in a <strong>.txt</strong> file, I got no problems but if I change extension to put <strong>.json</strong> I can't write on the file. </p>

<p>This is the working test code : </p>

<pre><code>#include &lt;SD.h&gt;
const int chipSelect = 4;
void setup()
{
 Serial.begin(115200);
 Serial.print("Initializing SD card...");
 // make sure that the default chip select pin is set to
 // output, even if you don't use it:
 pinMode(10, OUTPUT);
 // see if the card is present and can be initialized:
 if (!SD.begin(chipSelect)) {
 Serial.println("Card failed, or not present");
 // don't do anything more:
 return;
 }
 Serial.println("card initialized.");
}
void loop()
{
 String dataString = "";
 File dataFile = SD.open("datalog.txt", FILE_WRITE);
 if (dataFile) {
   dataFile.println(dataString);
   dataFile.close();
   Serial.println(dataString);
 }
 else {
  Serial.println("error opening datalog.txt");
 }
}
</code></pre>

<p>This is the code doesn't work : </p>

<pre><code>#include &lt;SD.h&gt;
const int chipSelect = 4;
void setup()
{
 Serial.begin(115200);
 Serial.print("Initializing SD card...");
 // make sure that the default chip select pin is set to
 // output, even if you don't use it:
 pinMode(10, OUTPUT);
 // see if the card is present and can be initialized:
 if (!SD.begin(chipSelect)) {
 Serial.println("Card failed, or not present");
 // don't do anything more:
 return;
 }
 Serial.println("card initialized.");
}
void loop()
{
 String dataString = "";
 File dataFile = SD.open("datalog.json", FILE_WRITE);
 if (dataFile) {
   dataFile.println(dataString);
   dataFile.close();
   Serial.println(dataString);
 }
 else {
  Serial.println("error opening datalog.txt");
 }
}
</code></pre>

<p>I have no idea where the problem come from</p>

<p>Thank you in advance </p>

<p>Thomas</p>