Python Forum
json.dumps output error in python3 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: json.dumps output error in python3 (/thread-23861.html)



json.dumps output error in python3 - prayuktibid - Jan-21-2020

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.


RE: json.dumps output error in python3 - buran - Jan-21-2020

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


RE: json.dumps output error in python3 - prayuktibid - Jan-21-2020

Thank you