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

后端 未结 13 1780
星月不相逢
星月不相逢 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 14:59

    In the Mac OS, install jq with the command,

    $ brew install jq
    

    You can get the pretty print JSON as similar as,

    $ curl -X GET http://localhost:8080/api/v1/appointments/1  | jq
    
    
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100   117    0   117    0     0   8404      0 --:--:-- --:--:-- --:--:--  9000
    {
      "craeted_at": "10:24:38",
      "appointment_date": "2019-02-08",
      "name_of_doctor": "Monika",
      "status": true,
      "price": 12.5,
      "id": 1
    }
    
    0 讨论(0)
  • 2020-12-07 15:01

    Shawn's solution but for Python 3:

    echo '{"foo": "bar"}' | python3 -m json.tool
    
    0 讨论(0)
  • 2020-12-07 15:02

    jq - a lightweight and flexible command-line JSON processor

    I felt this deserved its own entry when it took me longer than it should have to discover. I was looking for a simple way to pretty-print the json output of docker inspect -f. It was mentioned briefly above by Noufal Ibrahim as part of another answer.

    From the jq website (https://stedolan.github.io/jq/):

    jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.

    It provides colored output by default and you simply have to pipe to jq, e.g.

    cat file | jq . 
    

    Example:

    "Raw" json output vs the same piped to jq

    0 讨论(0)
  • 2020-12-07 15:03

    Formatting json as a table from the command line

    You can use jtab - a tool written in rust - to print any json data as a table.

    For example:

    ➜ echo '{"foo": "bar"}' | jtab
    
    +-----+
    | foo |
    +-----+
    | bar |
    +-----+
    

    It also works with a json array:

    ➜  echo '[{"id": "1", "name": "Rust"}, {"id": "2", "name": "Jtab"}]' | jtab
    
    +----+------+
    | id | name |
    +----+------+
    | 1  | Rust |
    +----+------+
    | 2  | Jtab |
    +----+------+
    
    0 讨论(0)
  • 2020-12-07 15:06

    You can use Python JSON tool (requires Python 2.6+).

    For example:

    echo '{ "element0" : "lorem", "element1" : "ipsum" }' | python -m json.tool
    

    Which will give you:

    {
        "element0": "lorem",
        "element1": "ipsum"
    }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题