Can someone suggest how I can beautify JSON in Python or through the command line?
The only online based JSON beautifier which could do it was: http://jsonviewer.stack.h
I didn't like the output of json.dumps(...) -> For my taste way too much newlines. And I didn't want to use a command line tool or install something. I finally found Pythons pprint (= pretty print). Unfortunately it doesn't generate proper JSON but I think it is useful to have a user friendly glympse at the stored data.
Output of json.dumps(json_dict, indent=4)
{
"hyperspace": {
"constraints": [],
"design": [
[
"windFarm.windparkSize.k",
"continuous",
[
0,
0,
5
]
],
[
"hydroPlant.primaryControlMax",
"continuous",
[
100,
300
]
]
],
"kpis": [
"frequency.y",
"city.load.p[2]"
]
},
"lhc_size": 10,
"number_of_runs": 10
}
Usage of pprint:
import pprint
json_dict = {"hyperspace": {"constraints": [], "design": [["windFarm.windparkSize.k", "continuous", [0, 0, 5]], ["hydroPlant.primaryControlMax", "continuous", [100, 300]]], "kpis": ["frequency.y", "city.load.p[2]"]}, "lhc_size": 10, "number_of_runs": 10}
formatted_json_str = pprint.pformat(json_dict)
print(formatted_json_str)
pprint.pprint(json_dict)
Result of pprint.pformat(...)
or pprint.pprint(...)
:
{'hyperspace': {'constraints': [],
'design': [['windFarm.windparkSize.k', 'continuous', [0, 0, 5]],
['hydroPlant.primaryControlMax',
'continuous',
[100, 300]]],
'kpis': ['frequency.y', 'city.load.p[2]']},
'lhc_size': 10,
'number_of_runs': 10}