Package for formatting JSON data in a coloured YAML-style, perfect for CLI output
npx @tessl/cli install tessl/npm-prettyjson@1.2.0prettyjson is a Node.js package that transforms JSON data into a colorized, YAML-style output format, perfect for command-line interfaces and console applications. It provides both programmatic API access and a command-line interface with extensive customization options for colors, indentation, and formatting styles.
npm install prettyjson (or npm install -g prettyjson for CLI)const prettyjson = require('prettyjson');ESM import (if supported):
import prettyjson from 'prettyjson';const prettyjson = require('prettyjson');
const data = {
username: 'alice',
projects: ['web-app', 'mobile-app'],
settings: {
theme: 'dark',
notifications: true
}
};
// Basic rendering
console.log(prettyjson.render(data));
// With custom options
console.log(prettyjson.render(data, {
keysColor: 'rainbow',
dashColor: 'magenta',
stringColor: 'white',
noColor: false
}));
// Parse and render JSON string
const jsonString = '{"name": "Bob", "age": 30}';
console.log(prettyjson.renderString(jsonString));
// Access version information
console.log('Using prettyjson version:', prettyjson.version);prettyjson is built around a simple, focused architecture:
Core function for converting JavaScript data structures into formatted, colorized YAML-style output.
/**
* Renders data as formatted, colorized YAML-style output
* @param data - Data to render (any type)
* @param options - Configuration options (optional, object)
* @param indentation - Base indentation level (optional, number, default: 0)
* @returns string - Formatted string output
*/
function render(data: any, options?: Options, indentation?: number): string;Function for parsing JSON strings and rendering them with formatting.
/**
* Parses JSON string and renders it with formatting
* @param data - JSON string to parse and render
* @param options - Configuration options (optional, object)
* @param indentation - Base indentation level (optional, number, default: 0)
* @returns string - Formatted string output or error message if invalid JSON
*/
function renderString(data: string, options?: Options, indentation?: number): string;Access to the package version.
/**
* Package version string accessed from the module
*/
const prettyjson = require('prettyjson');
const version: string = prettyjson.version;Complete CLI tool for processing JSON files and streams.
Usage:
# Render a JSON file
prettyjson package.json
# Process stdin
curl https://api.github.com/users/alice | prettyjson
# Interactive mode (no arguments)
prettyjsonCLI Options:
--keys COLOR # Set color for object keys
--dash COLOR # Set color for array dashes
--string COLOR # Set color for strings
--multiline_string COLOR # Set color for multiline strings
--number COLOR # Set color for numbers
--indent NUMBER # Set indentation spaces
--nocolor # Disable colors
--noalign # Disable value alignment
--escape # Escape conflicting characters
--inline-arrays # Render simple arrays inlineEnvironment Variables:
PRETTYJSON_KEYS - Default keys colorPRETTYJSON_DASH - Default dash colorPRETTYJSON_STRING - Default string colorPRETTYJSON_MULTILINE_STRING - Default multiline string colorPRETTYJSON_NUMBER - Default number colorPRETTYJSON_NUMBER_POSITIVE - Default positive number colorPRETTYJSON_NUMBER_NEGATIVE - Default negative number colorPRETTYJSON_INDENT - Default indentationPRETTYJSON_NOCOLOR - Disable colorsPRETTYJSON_NOALIGN - Disable alignmentPRETTYJSON_ESCAPE - Enable escapingPRETTYJSON_INLINE_ARRAYS - Enable inline arraysinterface Options {
/** Message displayed for empty arrays (default: '(empty array)') */
emptyArrayMsg?: string;
/** Color for object keys using colors.js syntax (default: 'green') */
keysColor?: string;
/** Color for array item dashes (default: 'green') */
dashColor?: string;
/** Color for all numbers (default: 'blue') */
numberColor?: string;
/** Color for positive numbers (default: uses numberColor) */
positiveNumberColor?: string;
/** Color for negative numbers (default: uses numberColor) */
negativeNumberColor?: string;
/** Color for strings (default: null - no coloring) */
stringColor?: string | null;
/** Color for multiline strings (default: null - no coloring) */
multilineStringColor?: string | null;
/** Indentation for nested objects in spaces (default: 2) */
defaultIndentation?: number;
/** Disable all coloring (default: false) */
noColor?: boolean;
/** Disable value alignment (default: false) */
noAlign?: boolean;
/** Escape conflicting characters with JSON.stringify (default: false) */
escape?: boolean;
/** Render undefined values instead of ignoring them (default: false) */
renderUndefined?: boolean;
/** Render simple arrays inline instead of multi-line (default: false) */
inlineArrays?: boolean;
}prettyjson handles all JavaScript data types:
"function() {}"Colors are specified using colors.js syntax:
Standard Colors: black, red, green, yellow, blue, magenta, cyan, white, gray, grey
Bright Colors: brightRed, brightGreen, brightYellow, brightBlue, brightMagenta, brightCyan, brightWhite
Special Effects: rainbow, zebra, america, trap, random
Styles: bold, dim, italic, underline, inverse, hidden, strikethrough
renderString() returns "Error: Not valid JSON!" in red text when JSON parsing failsrenderString() with empty string returns empty stringrenderString() with non-string input returns empty stringrenderUndefined: true option is set"function() {}" string representation