Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13951

How to get line breaks of a JSON attribute using Python?

$
0
0

JSON file :

{"items": [        {"item1": "/xyz"        },        {"item2": "/yzx"        },        {"item3": "/zxy"        }    ]}

Python Code :

def funct(request):    input = ''    # Request body    data = maybe_str(request.data())    if data:        if is_json(request.headers.get('Content-Type', '')):            input = json.dumps(data, indent=2, sort_keys=True,                           separators=(',', ': '))        else:            input = data    return input

The python function returns a formatted string and then places it within Sphinx CodeBlock.

Output :

{ "items": [ { "item1": "/xyz", "item2": "/yzx", "item3": "/zxy" }, { "item1": "/xyz", "item2": "/yzx", "item3": "/zxy" } ] }

Desired Output:

{"items": [    {"item1": "/xyz","item2": "/yzx","item3": "/zxy"    },    {"item1": "/xyz","item2": "/yzx","item3": "/zxy"    }  ]}

I tried using .replace('\\n','\n') from this stackoverflow issue but still didnt work.


Viewing all articles
Browse latest Browse all 13951

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>