CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-prettyjson

Package for formatting JSON data in a coloured YAML-style, perfect for CLI output

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

prettyjson

prettyjson 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.

Package Information

  • Package Name: prettyjson
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install prettyjson (or npm install -g prettyjson for CLI)

Core Imports

const prettyjson = require('prettyjson');

ESM import (if supported):

import prettyjson from 'prettyjson';

Basic Usage

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);

Architecture

prettyjson is built around a simple, focused architecture:

  • Core Rendering Engine: Recursive data structure traversal with type-specific formatting
  • Color System: Integration with colors.js for terminal color output
  • CLI Interface: Complete command-line tool with argument parsing and multiple input methods
  • Configuration System: Extensive options for customizing output appearance and behavior

Capabilities

Data Rendering

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;

String Parsing and Rendering

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;

Version Information

Access to the package version.

/**
 * Package version string accessed from the module
 */
const prettyjson = require('prettyjson');
const version: string = prettyjson.version;

Command Line Interface

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)
prettyjson

CLI 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 inline

Environment Variables:

  • PRETTYJSON_KEYS - Default keys color
  • PRETTYJSON_DASH - Default dash color
  • PRETTYJSON_STRING - Default string color
  • PRETTYJSON_MULTILINE_STRING - Default multiline string color
  • PRETTYJSON_NUMBER - Default number color
  • PRETTYJSON_NUMBER_POSITIVE - Default positive number color
  • PRETTYJSON_NUMBER_NEGATIVE - Default negative number color
  • PRETTYJSON_INDENT - Default indentation
  • PRETTYJSON_NOCOLOR - Disable colors
  • PRETTYJSON_NOALIGN - Disable alignment
  • PRETTYJSON_ESCAPE - Enable escaping
  • PRETTYJSON_INLINE_ARRAYS - Enable inline arrays

Configuration Options

interface 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;
}

Data Type Support

prettyjson handles all JavaScript data types:

  • Primitives: strings, numbers, booleans, null, undefined
  • Objects: Plain objects with proper key formatting and indentation
  • Arrays: Multi-line with dash prefixes, or inline mode for simple arrays
  • Functions: Rendered as "function() {}"
  • Dates: Rendered using default toString() representation
  • Errors: Special handling to display message and stack trace
  • Multiline Strings: Triple-quote formatting with proper indentation

Color System

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

Error Handling

  • Invalid JSON: renderString() returns "Error: Not valid JSON!" in red text when JSON parsing fails
  • Empty String Input: renderString() with empty string returns empty string
  • Non-string Input: renderString() with non-string input returns empty string
  • File Not Found: CLI displays error message in red and exits with code 1
  • Undefined Values: Ignored by default unless renderUndefined: true option is set
  • Function Values: Rendered as "function() {}" string representation
  • Circular References: May cause stack overflow (not explicitly handled)
  • Error Objects: Special handling to display message and stack trace properties

docs

index.md

tile.json