How to beautify JSON in Python?

后端 未结 13 1359
渐次进展
渐次进展 2021-01-29 18:55

Can someone suggest how I can beautify JSON in Python or through the command line?

The only online based JSON beautifier which could do it was: http://jsonviewer.stack.h

13条回答
  •  清酒与你
    2021-01-29 19:53

    From the command-line:

    echo '{"one":1,"two":2}' | python -mjson.tool
    

    which outputs:

    {
        "one": 1, 
        "two": 2
    }
    

    Programmtically, the Python manual describes pretty-printing JSON:

    >>> import json
    >>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
    {
        "4": 5,
        "6": 7
    }
    

提交回复
热议问题