How can I pretty-print a JSON file from the command line?

后端 未结 13 1833
星月不相逢
星月不相逢 2020-12-07 14:32

I\'ve a file with a sequence of JSON element:

{ element0: \"lorem\", value0: \"ipsum\" }
{ element1: \"lorem\", value0: \"ipsum\" }
...
{ elementN: \"lorem\"         


        
13条回答
  •  半阙折子戏
    2020-12-07 15:06

    with python (2 and 3):

    alias prettify_json="python -c 'import sys ;import json ; print(json.dumps(json.loads(sys.stdin.read()), indent=4))'"
    

    or with ruby:

    alias prettify_json="ruby -e \"require 'json';puts JSON.pretty_generate(JSON.parse(STDIN.read))\""
    

    you can use:

    echo '{"bar": "abc", "foo": "def"}' | prettify_json
    
    curl http://.../file.json | prettify_json
    

提交回复
热议问题