How to prettyprint a JSON file?

前端 未结 13 806
滥情空心
滥情空心 2020-11-22 01:53

I have a JSON file that is a mess that I want to prettyprint. What\'s the easiest way to do this in Python?

I know PrettyPrint takes an \"object\", which I think can

13条回答
  •  爱一瞬间的悲伤
    2020-11-22 02:16

    Use pprint: https://docs.python.org/3.6/library/pprint.html

    import pprint
    pprint.pprint(json)
    

    print() compared to pprint.pprint()

    print(json)
    {'feed': {'title': 'W3Schools Home Page', 'title_detail': {'type': 'text/plain', 'language': None, 'base': '', 'value': 'W3Schools Home Page'}, 'links': [{'rel': 'alternate', 'type': 'text/html', 'href': 'https://www.w3schools.com'}], 'link': 'https://www.w3schools.com', 'subtitle': 'Free web building tutorials', 'subtitle_detail': {'type': 'text/html', 'language': None, 'base': '', 'value': 'Free web building tutorials'}}, 'entries': [], 'bozo': 0, 'encoding': 'utf-8', 'version': 'rss20', 'namespaces': {}}
    
    pprint.pprint(json)
    {'bozo': 0,
     'encoding': 'utf-8',
     'entries': [],
     'feed': {'link': 'https://www.w3schools.com',
              'links': [{'href': 'https://www.w3schools.com',
                         'rel': 'alternate',
                         'type': 'text/html'}],
              'subtitle': 'Free web building tutorials',
              'subtitle_detail': {'base': '',
                                  'language': None,
                                  'type': 'text/html',
                                  'value': 'Free web building tutorials'},
              'title': 'W3Schools Home Page',
              'title_detail': {'base': '',
                               'language': None,
                               'type': 'text/plain',
                               'value': 'W3Schools Home Page'}},
     'namespaces': {},
     'version': 'rss20'}
    

提交回复
热议问题