I want to print the Json in a nice way, I want to get rid of the brackets, quotes and braces and only use indents and line-endings to show the json's structure.
for example if I have a Json like this:
{
"A": {
"A1": 1,
"A2": 2
},
"B": {
"B1": {
"B11": {
"B111": 1,
"B112": 2
},
"B12": {
"B121": 1,
"B122": 2
}
},
"B2": {
"B21": [1,2],
"B22": [1,2]
}
},
"C": "CC"
}
How could I print the json by removing {} and [], what I want is:
A:
A1: 1
A2: 2
B:
B1:
B11:
B111: 1
B112: 2
B12:
B121: 1
B122: 2
B2:
B21: 1, 2
B22: 1, 2
C: CC
You can load the json into a python object, and then convert the python object to YAML. The other solution is to simply iterate over the dictionaries and format it however you want.
Here's an example of converting it to YAML. It doesn't give you precisely what you want, but it's pretty close. There are lots of ways to customize the output, this is just a quick hack to show the general idea:
import json
import yaml
data = json.loads('''
{
"A": {
"A1": 1,
"A2": 2
},
"B": {
"B1": {
"B11": {
"B111": 1,
"B112": 2
},
"B12": {
"B121": 1,
"B122": 2
}
},
"B2": {
"B21": [1,2],
"B22": [1,2]
}
},
"C": "CC"
}
''')
print yaml.safe_dump(data, allow_unicode=True, default_flow_style=False)
This is the output I get:
A:
A1: 1
A2: 2
B:
B1:
B11:
B111: 1
B112: 2
B12:
B121: 1
B122: 2
B2:
B21:
- 1
- 2
B22:
- 1
- 2
C: CC
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