[solved] How can I have a \n for JSON Line

Hello

I am writing a example file and my goal is to save some measurement into a jsonl file

Example:

{"mod":"demo","date":"24-07-05","sensors":[{"temp":"22","altitude":"454"}]}
{"mod":"demo","date":"24-07-06","sensors":[{"temp":"23","altitude":"454"}]}
{"mod":"demo","date":"24-07-07","sensors":[{"temp":"24","altitude":"454"}]}
{"mod":"demo","date":"24-07-08","sensors":[{"temp":"23","altitude":"454"}]}

selon JSON Line, each

{"mod":"demo","date":"24-07-05","sensors":[{"temp":"22","altitude":"454"}]}

must be on one line. So at the end of the entry, we need to have a \n

If we look at my script here,

  1. I open the file
  2. I serialized my object and I save the string directly into my file. I followed that example (See "write to a file")
  3. Then I close my file.

If now I look at my file, each entry follow the previous entry. All is in one line.

File sd_log;  
sd_log = sd.open(fileName, O_RDWR | O_CREAT | O_AT_END);      // Open the file log.jsonl
serializeJson(jdoc,sd_log);                                   // serialize and write te directly to the file  
sd_log.close();

My question, is there a way to tell serializejson() to append a \n ?

Thanks

OK, forget this topics.
Simply add sd_log.println();

File sd_log;  
sd_log = sd.open(fileName, O_RDWR | O_CREAT | O_AT_END);      // Open the file log.jsonl
serializeJson(jdoc,sd_log);                                   // serialize and write te directly to the file  
sd_log.println();
sd_log.close();

That’s adding both CR ‘\r’ and LF ‘\n’

If you only want LF then you can just do sd_log.write('\n');

EDIT: NOT SUITABLE FOR A JSON LINES FORMAT

Note that instead of serializeJson() you could also use serializeJsonPretty() which will add line breaks and spaces etc to make you JSON look nice

JSON Lines is a distinct record-per-line format, sort of like CSV, but with more flexibility and a lot more quotation marks.

  • no difference between \r\n and \n -- extra whitespace outside of a data value is ignored in all cases
  • pretty-printing would change the meaning

thanks I had missed the small l at the end of

Hello
Thansk for your replies. This make sense:
sd_log.write('\n');
Thanks