prettyjson

repository·master·Indexed 19 days ago

https://github.com/rafeca/prettyjson

A package for formatting JSON data into a colored, YAML-style output optimized for human readability in CLI environments. Version 1.2.5 provides a CLI tool for decoding files, stdin, or interactive strings, and a Node.js library via the render(data, options) method.

Tokens
1.5K
Snippets
6
Records
6
Agent score
17%

What's inside prettyjson

  1. Use prettyjson from the CLI

    master

    The prettyjson CLI can be used to format JSON in three ways:

    1. Decode a JSON file: Pass the file path as the first argument.
    2. Decode stdin: Pipe command output (like curl) into the CLI.
    3. Decode random strings: Run the CLI without arguments to enter an interactive prompt where you can paste JSON strings.
    # Decode a file
    $ prettyjson package.json
    
    # Decode stdin
    $ curl https://api.github.com/users/rafeca | prettyjson
    
    # Interactive mode
    $ prettyjson
  2. Use prettyjson via the CLI

    master

    The prettyjson CLI tool allows you to decode and format JSON data for better readability. It supports two primary input modes:

    1. File Input: Pass a filename as the first argument to read and format the contents of that file.
    2. Standard Input (stdin): Pipe JSON data into the command or type it interactively. The tool processes data based on newline delimiters or when the stream ends.

    Example usage:

    # Format a specific file
    prettyjson data.json
    
    # Pipe JSON from another command
    cat data.json | prettyjson
    prettyjson data.json
    # or
    cat data.json | prettyjson
  3. Use prettyjson in Node.js

    master

    Import prettyjson and call the render(data, options) method. The method returns a formatted string of the provided JSON data.

    Available options keys:

    • noColor: Boolean to disable colors.
    • keysColor: Color for hash keys (using colors.js syntax).
    • dashColor: Color for array dashes.
    • stringColor: Color for strings.
    • multilineStringColor: Color for multiline strings.
    var prettyjson = require('prettyjson');
    
    var data = {
      username: 'rafeca',
      url: 'https://github.com/rafeca',
      twitter_account: 'https://twitter.com/rafeca',
      projects: ['prettyprint', 'connfu']
    };
    
    // Basic usage with options
    var options = {
      noColor: true
    };
    console.log(prettyjson.render(data, options));
    
    // Advanced usage with custom colors
    console.log(prettyjson.render(data, {
      keysColor: 'rainbow',
      dashColor: 'magenta',
      stringColor: 'white',
      multilineStringColor: 'cyan'
    }));
  4. Reference CLI options for prettyjson

    master

    Customize the CLI output using the following options:

    OptionDescription
    --string=<color>Set color for strings (e.g., red)
    --multiline_string=<color>Set color for multiline strings (e.g., cyan)
    --keys=<color>Set color for hash keys (e.g., blue)
    --dash=<color>Set color for array dashes (e.g., yellow)
    --number=<color>Set color for numbers (e.g., green)
    --nocolor=1Disable color output
    --indent=<number>Set indentation level
    --inline-arrays=1Render array elements in a single line
    --escape=1Escape conflictive strings

    Note: Configuration via environment variables is deprecated and will be removed in version 1.0.0.

    # Example: Custom colors
    $ prettyjson --string=red --multiline_string=cyan --keys=blue --dash=yellow --number=green package.json
    
    # Example: No color
    $ prettyjson --nocolor=1 package.json
    
    # Example: Indentation
    $ prettyjson --indent=4 package.json
    
    # Example: Inline arrays
    $ prettyjson --inline-arrays=1 package.json
    
    # Example: Escape strings
    $ prettyjson --escape=1 package.json
  5. Configure prettyjson CLI options

    master

    You can customize the output of the prettyjson CLI using command line flags or environment variables. The CLI prioritizes command line flags over environment variables.

    Command Line Flags

    FlagEnvironment VariableDescription
    --keysPRETTYJSON_KEYSColor for JSON keys
    --dashPRETTYJSON_DASHColor for dashes
    --indentPRETTYJSON_INDENTIndentation level
    --stringPRETTYJSON_STRINGColor for strings
    --multiline_stringPRETTYJSON_MULTILINE_STRINGColor for multiline strings
    --numberPRETTYJSON_NUMBERColor for numbers
    --numberPRETTYJSON_NUMBER_POSITIVEColor for positive numbers
    --numberPRETTYJSON_NUMBER_NEGATIVEColor for negative numbers
    --nocolorPRETTYJSON_NOCOLORDisable color output
    --noalignPRETTYJSON_NOALIGNDisable alignment
    --escapePRETTYJSON_ESCAPEEscape conflictive strings
    --inline-arraysPRETTYJSON_INLINE_ARRAYSRender array elements in a single line

    Note: The --number flag is used to set the base color for numbers, while PRETTYJSON_NUMBER_POSITIVE and PRETTYJSON_NUMBER_NEGATIVE provide specific overrides for signed values.

    prettyjson --keys blue --indent 2 --nocolor data.json