I'm fairly new to Arduino so I used a sample code before integrating this into my main code. I'm printing my value in a JSON format over serial. My Arduino outputs this and the python receives this and prints it in the same format


"value" : 5


but when I use python to try writing to a JSON file on my computer, I'm not getting the same JSON format. Can anyone figure out what's wrong? I need to write to my JSON file in JSON format, but instead, I'm getting this format written in my JSON file " \"value\": 5\r\n" instead of {"value: 5"}. Another problem I'm getting is that sometimes the values I get in my JSON get cut off so sometimes I end up with \r\n" or other parts of the current output. My goal is to get the python code to overwrite the JSON file in real-time, so whenever I open the JSON file, the entire output should be there in the correct JSON format. Any help is appreciated, thank you.

Arduino Code:

#include <ArduinoJson.h>

int analogPin = 3;     
int data = 0; 
char userInput;          

StaticJsonDocument<100> testDocument;
char buffer[100];

void setup(){

  Serial.begin(9600);          //  setup serial

  StaticJsonDocument<100> testDocument;

  testDocument["sensorType"] = "Temperature";
  testDocument["value"] = 10;

  char buffer[100];

  serializeJsonPretty(testDocument, buffer);


void loop(){

    testDocument["value"] = i;

    serializeJsonPretty(testDocument, buffer);



Python Code:

import serial
import json
import time

ser = serial.Serial('COM4', baudrate = 9600, timeout=1)

while 1:

    data = (ser.readline().decode('ascii')) #split('\r\n')

    #j = json.loads(data)

    file = "C:/Users/Mike/Desktop/test.json"

    my_data_file = open(file, 'w')

    with open(file1, "w") as my_data_file :
        json.dump(data, my_data_file)

  • 2
    Is the JSON correctly send via serial? Please look at the output of the Serial Monitor, to check that. Having backslashes in there suggest, that the ASCII characters don't get escaped correctly. The python code is not really our territory (as here is Arduino SE). Though: you have commented out the json.loads(data) line. Currently you are trying to json dump a string, which should be a data structure. if you want to dump the json, you first need to load it. Or you could completely ditch the python json and just write the received json string to the file
    – chrisl
    Nov 12 '20 at 7:50
  • Yes, when I print the data I'm receiving the same output as my serial monitor, just not in the JSON file. I'll take a look at the code again and look at what you suggested. I'll try posting to a more suitable group for the python as well. Thank you for your help.
    – Mike
    Nov 12 '20 at 14:23
  • @chrisl I looked at what you said and so every time I run the code, I get a JSON output in the serial monitor and when I print the data in python. However, when I write the data to the JSON file it's in that weird format with the quotations on the outside and backslash symbol in between. I couldn't find a way to write it in the JSON format. I used a different arduino code to try it and I'm still getting the same thing, no luck.
    – Mike
    Nov 12 '20 at 21:07
  • 1
    I don't know, what you are doing with your python code. But in the above code, you should do the following: Read all available data, until you have all you need, not just one line at a time. Then use the line j = json.loads(data) to get the data as python data structure (data must be a string with the complete json string). Then you can dump j to the file. You are getting the masking backslashes, because you are currently trying to dump a string instead of the data structure. And when saving a string in json, the json module must escape the special characters like "
    – chrisl
    Nov 12 '20 at 21:13
  • okay, I think that's where I'm getting my errors with the special characters. I'll take a look. Thank you for helping out.
    – Mike
    Nov 12 '20 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.