How to convert json into human readable format

5.5k Views Asked by At

This is my json.

initiated with options {'record_id': 335, 'backup_config_id': 28, 'attachement_type': 3, 
'instance_id': 48, 'volumes': [{'aws_instance_id': 'xyz', 'aws_instance_name': 'abc', 'instance_record_id': 48, 'aws_region': 'ap-southeast-2', 
'aws_volume_id': 'abc', 'aws_snapshot_id': 'snap-123', 
'aws_zone': 'ap-southeast-2b', 'aws_capacity': 16, 'aws_device': '/dev/sda1', 'volume_type': 'gp2', 'iops': 100, 
'encrypted': False, 'delete_on_termination': False, 'end_time': '2020-05-09T16:18:55.962000Z', 'success': 1, 
'deletion_time': None, 'aws_root_device': '/dev/sda1',
'volume_id': 'vol-123', 'device': '/dev/sda1'}, {'aws_instance_id': 'i-123',
 'aws_instance_name': 'abc', 'instance_record_id': 48, 
'aws_region': 'ap-southeast-2', 'aws_volume_id': 'vol-123', 
'aws_snapshot_id': 'snap-123', 'aws_zone': 'ap-southeast-2b', 'aws_capacity': 16, 'aws_device': '/dev/sdh', 'volume_type': 'gp2', 'iops': 100, 'encrypted': False, 'delete_on_termination': False, 'end_time': '2020-05-09T16:18:55.962000Z', 'success': 1, 'deletion_time': None, 'aws_root_device': '/dev/sda1','volume_id': 'vol-123', 'device': '/dev/sdh'}], 'aws_instance': 'i-123'}

User can't understand this. I want to make this readable in python.

How to make it?

3

There are 3 best solutions below

1
Aidan On
2
Darshit Kothari On

The json module in python already implements some basic pretty printing with the indent parameter:

import json
parsed = json.loads(your_json)
print(json.dumps(parsed, indent=2, sort_keys=True))
0
KansaiRobot On

@Darshit Kothari answer did not work for me so I tried

import json

with open('yourjson.json') as f:
    parsed=json.load(f)

print(json.dumps(parsed, indent=4))