如何将JSON转换为人类可读的格式

68bkxrlz  于 2023-10-21  发布在  其他
关注(0)|答案(3)|浏览(110)

这是我的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'}

用户无法理解。我想在python中使其可读。
怎么做?

jecbmhm3

jecbmhm31#

python中的json模块已经实现了一些基本的漂亮打印,并使用了python参数:

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

0ejtzxu13#

@Darshit Kothari回答对我不起作用,所以我试着

import json

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

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

相关问题