Install prettyjson via npm
masterInstall prettyjson globally using npm to add the prettyjson command to your PATH.
$ npm install -g prettyjsonrepository·master·Indexed 19 days ago
https://github.com/rafeca/prettyjsonA 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.
Install prettyjson globally using npm to add the prettyjson command to your PATH.
$ npm install -g prettyjsonThe prettyjson CLI can be used to format JSON in three ways:
curl) into the CLI.# Decode a file
$ prettyjson package.json
# Decode stdin
$ curl https://api.github.com/users/rafeca | prettyjson
# Interactive mode
$ prettyjsonThe prettyjson CLI tool allows you to decode and format JSON data for better readability. It supports two primary input modes:
Example usage:
# Format a specific file
prettyjson data.json
# Pipe JSON from another command
cat data.json | prettyjsonprettyjson data.json
# or
cat data.json | prettyjsonImport 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'
}));Customize the CLI output using the following options:
| Option | Description |
|---|---|
--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=1 | Disable color output |
--indent=<number> | Set indentation level |
--inline-arrays=1 | Render array elements in a single line |
--escape=1 | Escape 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.jsonYou can customize the output of the prettyjson CLI using command line flags or environment variables. The CLI prioritizes command line flags over environment variables.
| Flag | Environment Variable | Description |
|---|---|---|
--keys | PRETTYJSON_KEYS | Color for JSON keys |
--dash | PRETTYJSON_DASH | Color for dashes |
--indent | PRETTYJSON_INDENT | Indentation level |
--string | PRETTYJSON_STRING | Color for strings |
--multiline_string | PRETTYJSON_MULTILINE_STRING | Color for multiline strings |
--number | PRETTYJSON_NUMBER | Color for numbers |
--number | PRETTYJSON_NUMBER_POSITIVE | Color for positive numbers |
--number | PRETTYJSON_NUMBER_NEGATIVE | Color for negative numbers |
--nocolor | PRETTYJSON_NOCOLOR | Disable color output |
--noalign | PRETTYJSON_NOALIGN | Disable alignment |
--escape | PRETTYJSON_ESCAPE | Escape conflictive strings |
--inline-arrays | PRETTYJSON_INLINE_ARRAYS | Render 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