I have this code:
import json
my_list = [1, 2, 3]
my_dict = {"key": "value", "boolean": True}
my_json = {"object": my_dict, "array": my_list}
print(json.dumps(my_json, indent=4))
I get an output like:
{
"object": {
"key": "value",
"boolean": true
},
"array": [
1,
2,
3
]
}
I want it the elements of the "array"
array to appear on the same line, like so:
{
"object": {
"key": "value",
"boolean": true
},
"array": [1, 2, 3]
}
How can I get this result?
Your task can be fulfilled by using a library like jsbeautifier
Install the library by using:
pip install jsbeautifier
Then add the options and call the jsbeautifier.beautify()
function.
Full Code:
import json
import jsbeautifier
my_list = [1, 2, 3]
my_dict = {"key": "value", "boolean": True}
my_json = {"object": my_dict, "array": my_list}
options = jsbeautifier.default_options()
options.indent_size = 2
print(jsbeautifier.beautify(json.dumps(my_json), options))
Output:
{
"object": {
"key": "value",
"boolean": true
},
"array": [1, 2, 3]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With