Python Forum

Full Version: json.dumps output error in python3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to send a dict file in JSON fromat usin "ujson.dumps" method to local http server. After executing ujson.dumps the output get changes.

This is my code
import ujson
 
file ={
    "success": true,
    "status_code": 200,
    "version": "1.2.0003",
    "path": "/get_capture_options",
    "method": "GET",
    "capture_options": [
        {
            "width": 1920,
            "framerate": 30,
            "height": 1080
        },
        {
            "width": 1280,
            "framerate": 30,
            "height": 720
        },
        {
            "width": 1280,
            "framerate": 60,
            "height": 720
        },
        {
            "width": 640,
            "framerate": 30,
            "height": 480
        },
        {
            "width": 640,
            "framerate": 60,
            "height": 480
        },
        {
            "width": 640,
            "framerate": 90,
            "height": 480
        }
    ]
}

file2 = ujson.dumps(file).encode('utf-8')
print(file2)
This is the output:
Quote: b'{"success":true,
"status_code":200,
"method":"GET",
"path":"\\/get_capture_options",
"version":"1.2.0003",
"capture_options":[{"width":1920,"height":1080,"framerate":30},
{"width":1280,"height":720,"framerate":30},
{"width":1280,"height":720,"framerate":60},
{"width":640,"height":480,"framerate":30},
{"width":640,"height":480,"framerate":60},
{"width":640,"height":480,"framerate":90}]}'
You can seen in path parameter the "\\" added. Why is this happening? how to solve this? Please help.
Looking at the docs, there is option escape_forward_slashes, which controls whether forward slashes (/) are escaped. Default is True.

If you are going to use third-party package, at least make an effort to read the [relevant part of] the docs
Thank you