I have json file like this, How can I write a code to format above json file that looks same as below
{'alerts': [{'id': 573983, 'version': 2, 'createdOn': 1615530258000, 'modifiedOn':
1615530763000, 'type': 'EVENT', 'name': '[IBM]: Usage Tier Changed', 'description': 'A host
changed to a higher usage tier', 'enabled': True, 'criteria': {'text': 'Hosts in tier have
increased', 'source': None, 'severity': None, 'query': None, 'scope': None}, 'severity': 4,
'timespan': 600, 'customNotification': {'titleTemplate': '{{__alert_name__}} is
{{__alert_status__}}', 'useNewTemplate': True}, 'notificationCount': 0, 'teamId': 36050,
'autoCreated': True, 'rateOfChange': False, 'reNotifyMinutes': 30, 'reNotify': False,
'invalidMetrics': [], 'groupName': 'default', 'valid': True, 'severityLabel': 'LOW',
'condition': 'count(customEvent) > 0', 'customerId': 45992, 'lastCheckTimeInMs':
1638964620000}, {'id': 576757, 'version': 1, 'createdOn': 1616520901000, 'modifiedOn':
1616520901000, 'type': 'MANUAL', 'name': 'Root volume disk usage warning', 'description':
'Root volume disk usage warning', 'enabled': False, 'severity': 4, 'timespan': 300,
'customNotification': {'titleTemplate': '{{__alert_name__}} is {{__alert_status__}} on
host.mac = {{host.mac}}', 'useNewTemplate': True}, 'notificationCount': 0, 'teamId': 36050,
'autoCreated': True, 'rateOfChange': False, 'reNotifyMinutes': 30, 'reNotify': False,
'invalidMetrics': [], 'groupName': 'default', 'valid': True, 'severityLabel': 'LOW',
'segmentBy': ['host.mac'], 'segmentCondition': {'type': 'ANY'}, 'condition':
'avg(avg(fs.root.used.percent)) > 80', 'customerId': 45992}]}
How can I write a code to format above json file that looks same as below I have json file like this, How can I write a code to format above json file that looks same as below
{
"alerts":[
{
"id":573983,
"version":2,
"createdOn":1615530258000,
"modifiedOn":1615530763000,
"type":"EVENT",
"name":"[IBM]: Usage Tier Changed",
"description":"A host changed to a higher usage tier",
"enabled":true,
"criteria":{
"text":"Hosts in tier have increased",
"source":"None",
"severity":"None",
"query":"None",
"scope":"None"
},
"severity":4,
"timespan":600,
"customNotification":{
"titleTemplate":"{{__alert_name__}} is {{__alert_status__}}",
"useNewTemplate":true
},
"notificationCount":0,
"teamId":36050,
"autoCreated":true,
"rateOfChange":false,
"reNotifyMinutes":30,
"reNotify":false,
"invalidMetrics":[
],
"groupName":"default",
"valid":true,
"severityLabel":"LOW",
"condition":"count(customEvent) > 0",
"customerId":45992,
"lastCheckTimeInMs":1638964620000
},
{
"id":576757,
"version":1,
"createdOn":1616520901000,
"modifiedOn":1616520901000,
"type":"MANUAL",
"name":"Root volume disk usage warning",
"description":"Root volume disk usage warning",
"enabled":false,
"severity":4,
"timespan":300,
"customNotification":{
"titleTemplate":"{{__alert_name__}} is {{__alert_status__}} on host.mac = {{host.mac}}",
"useNewTemplate":true
},
"notificationCount":0,
"teamId":36050,
"autoCreated":true,
"rateOfChange":false,
"reNotifyMinutes":30,
"reNotify":false,
"invalidMetrics":[
],
"groupName":"default",
"valid":true,
"severityLabel":"LOW",
"segmentBy":[
"host.mac"
],
"segmentCondition":{
"type":"ANY"
},
"condition":"avg(avg(fs.root.used.percent)) > 80",
"customerId":45992
}
]
}
How can I write a code to format above json file that looks same as below
CodePudding user response:
Use Json and pprint libs
import json
import pprint
json_data = None
with open('file_name.txt', 'r') as f:
data = f.read()
json_data = json.loads(data)
prettyjson = pprint.pprint(json_data)
CodePudding user response:
This will do the job
import pprint
json_data = '';
pprint.pprint(json_data);