I am currently Working on a project, where I save values in a JSON File on an SD Card.

The JSON looks something like this:

{ "ID1":{[[starttime, endtime],
          [starttime, endtime],
  "ID2":{[[starttime, endtime],
          [starttime, endtime],
          [starttime, endtime],

I have to add start and endtimes to IDs, so I have to edit the file. Is there a nicer/easier/better way than the following?:

  1. Create Buffer (e.g. 512 Bytes)
  2. Read 512 Bytes from file to buffer
  3. Edit/Add Values in Buffer
  4. Save Buffer to another File
  5. Repeat 2. to 4. until end of file
  6. Remove old File and rename new File

I am currently using the ESP32 Core SD.h Libary

  • you could seek() in the file and overwrite bytes in the file. but not add or remove bytes.
    – Juraj
    Commented May 10, 2020 at 9:17
  • To add data, no, there is no better way. The way you describe is called "stream editing" and is the normal way on low-memory machines. TBH I'd work in lines, not blocks of 512 bytes.
    – Majenko
    Commented May 10, 2020 at 9:22
  • 1
    Also TBH I wouldn't use JSON. It's not great (as you see) at editing. Instead a simple CSV format would be better where you can just append data to the end of the file, then collate it later on the computer in excel.
    – Majenko
    Commented May 10, 2020 at 9:33
  • Going by line is a good Idea! I will give it a try. I need a JSON File, because I want to use the data in a React App. The reason why I use the SD Card is in case of a failure I want to be able to retrieve the data. Thanks a lot!
    – Markus
    Commented May 10, 2020 at 9:44
  • you can use notepad++ text editor to sort a csv file and to add JSON formatting to the columns
    – jsotola
    Commented May 10, 2020 at 17:41


Your Answer

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

Browse other questions tagged or ask your own question.