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

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

提交回复
热议问题