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

后端 未结 13 1782
星月不相逢
星月不相逢 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:07

    You can use jq package which can be installed in all Linux systems. Install the tool using below commands.

    # Redhat based systems(Centos)
    yum install -y epel-release
    yum install -y jq
    
    # Debian based systems
    apt install -y jq
    

    Then you will be able to pipe text streams to the jq tool.

    echo '{"test":"value", "test2":"value2"}' | jq
    

    Hope this answer will help.

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

    There are a bunch of them. I personally have this alias in my .zshrc

    pjson () {
            ~/bin/pjson.py | less -X
    }
    

    where pjson.py is

    #!/usr/bin/env python
    
    import json
    import sys
    
    try:
        input_str = sys.stdin.read()
        print json.dumps(json.loads(input_str), sort_keys = True, indent = 2)
    except ValueError,e:
        print "Couldn't decode \n %s \n Error : %s"%(input_str, str(e))
    

    Allows me to use that in a command line as a pipe (something like curl http://.... | pjson).

    OTOH, Custom code is a liability so there's jq, which to me looks like the gold standard. It's written in C (and is hence portable with no dependencies like Python or Node), does much more than just pretty printing and is fast.

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

    Pipe the results from the file into the python json tool 2.6 onwards

    cat 'file_name' | python -m json.tool
    
    0 讨论(0)
  • 2020-12-07 15:14

    To format your JSON with proper indentation use JSON.stringify

    console.log(JSON.stringify(your_object, null, 2)); // prints in b/w
    

    But to make it prettier by adding colors, you can check out my package beautify-json

    beautify-json

    Example:

    const { jsonBeautify } = require('beautify-json')
    
    let your_object = {
        name: 'Nikhil',
        age: 22,
        isMarried: false,
        girlfriends: null,
        interestedIn: [
            'javascript',
            'reactjs',
            'nodejs'
        ]
    }
    
    jsonBeautify(your_object) // It will beautify your object with colors and proper indentation and display it on the terminal
    

    Output: Screenshot of the beautified object printed in terminal

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

    Colored output using Pygmentize + Python json.tool

    Pygmentize is a killer tool. See this. I combine python json.tool with pygmentize

    echo '{"foo": "bar"}' | python -m json.tool | pygmentize -g
    

    For other similar tools and installation instruction see the answer linked above.

    Here is a live demo:

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

    From a mac OS 10.15 terminal I can use json_pp:

    echo '{ "element0" : "lorem", "element1" : "ipsum" }' | json_pp
    
    0 讨论(0)
提交回复
热议问题