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.
Edit: I have changed some variable names. I used "input" here just as an example
This solution works for me in a python3 shell. In case this does not help, you can try using
simplejson,ujson, orjson5, which all are also able to format JSON.And also, you should not use
inputas a variable name, because it's a reserved word — try usingjson_inputinstead.