1

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?

Thanks in advance :)

codebuff
  • 93
  • 3
  • 15
  • Maybe this is what you are looking for? https://stackoverflow.com/questions/12943819/how-to-prettyprint-a-json-file – René Jahn May 11 '20 at 05:49
  • @Minato Thank you for your response.I'll try it – codebuff May 11 '20 at 05:51
  • @Minato Is there anyway to store the pprint format? – codebuff May 11 '20 at 07:02
  • If by store you mean persisting to disk, then this might help: https://stackoverflow.com/questions/5214578/print-string-to-text-file – René Jahn May 11 '20 at 07:20
  • @Minato I am asking pprint fomat. Is any possibility to store pprint format in a variable.Because I need to show this data in frontend. For customers,we have to make it human readable not for me.Please help me with this – codebuff May 11 '20 at 08:07

3 Answers3

3

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

Pretty print, pprint is one way to do it

Aidan
  • 413
  • 3
  • 22
0

@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))
KansaiRobot
  • 7,564
  • 11
  • 71
  • 150