Req Body auto change issue

Hi All,

I’m trying to read the ESP32 data in the form of JSON with pyserial and revert the same JSON back to the notecard with the same python script. But I can see some error logs. It seems some \ symbols added in between the data. In my Req there is no \ symbols.

Python Script:

import json
import notecard
import serial
import time

productUID = "com.gmail.pradeeplogu26:blues_wio_anomaly_detector"

if __name__ == '__main__':
    ser = serial.Serial('COM22', 115200, timeout=1)
    ser.flush()
    serial = serial.Serial('COM7', 9600)
    card = notecard.OpenSerial(serial, debug=True)

    req = {"req": "hub.set"}
    req["product"] = productUID
    req["mode"] = "continuous"
    card.Transaction(req)

    while True:
        if ser.in_waiting > 0:
            line = ser.readline().decode('utf-8').rstrip()
            print("Serial Data is" + str(line))
            req = {"req": "note.add"}
            req["file"] = "sensors.qo"
            req["start"] = True
            req["body"] = str(line)
            print("Req is : " + str(req))
            card.Transaction(req)
            time.sleep(5)

Error Log: b’{“err”:“body: unmarshal: field body expects object_raw {io}”}’

eventhough I tried to replace " with ’ in the received JSON but the issue remains same.

Hi @Pradeep,

The backslashes are escape characters to allow for a proper display of JSON output. Check out this Stack Overflow post on a similar issue. I haven’t tested this, but my guess is this will help solve your problem:

json_data = json.dumps(response_read)
json_without_slash = json.loads(json_data)

Hi Rob,

Thanks for the help, I tried to load the JSON and again send to notecard now the return response doesn’t contain backslash, and it’s working.

import json
import notecard
import serial
import time

productUID = ""

if __name__ == '__main__':
    ser = serial.Serial('COM22', 115200, timeout=1)
    ser.flush()
    serial = serial.Serial('COM7', 9600)
    card = notecard.OpenSerial(serial, debug=True)

    req = {"req": "hub.set"}
    req["product"] = productUID
    req["mode"] = "continuous"
    card.Transaction(req)

    while True:
        if ser.in_waiting > 0:
            line = ser.readline().decode('utf-8').rstrip()
            json_without_slash = json.loads(line)
            print("Serial Data is " + str(json_without_slash))
            
            req = {"req": "note.add"}
            req["file"] = "sensors.qo"
            req["start"] = True
            req["body"] = json_without_slash
            
            print("Req is : " + str(req))
            card.Transaction(req)
            time.sleep(5)
1 Like