CLI JSON Formatter

1.2.5 · active · verified Wed Apr 22

prettyjson is a Node.js package designed to format JSON data into a human-readable, colored YAML-style output, primarily for command-line interface (CLI) applications. It offers both a programmatic API for Node.js scripts and a direct CLI tool for processing JSON files, `stdin`, or interactive input. The current stable version is 1.2.5. Release cadence appears sporadic, with the last update over a year ago, but still maintained with dependency fixes. Its key differentiator lies in its focus on visually appealing, colored output directly in the terminal, making debugging and data inspection easier compared to raw JSON. It allows extensive customization of colors and formatting options, including indentation and inline arrays. It depends on the `colors` library for terminal output styling.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically use `prettyjson` to render a complex JavaScript object into a colored, YAML-style string, customizing colors and indentation.

const prettyjson = require('prettyjson');

const complexData = {
  user: {
    id: 'usr_xyz123',
    name: 'Alice Wonderland',
    email: 'alice@example.com',
    isActive: true,
    roles: ['admin', 'editor'],
    lastLogin: new Date('2024-04-20T14:30:00Z'),
    preferences: {
      theme: 'dark',
      notifications: {
        email: true,
        sms: false
      }
    }
  },
  posts: [
    { id: 1, title: 'First Post', content: 'Lorem ipsum...', tags: ['js', 'cli'], published: true },
    { id: 2, title: 'Second Post', content: 'Dolor sit amet...', tags: ['node', 'format'], published: false }
  ],
  config: {
    apiEndpoint: process.env.API_URL ?? 'https://api.example.com/v1',
    timeoutMs: 5000
  }
};

const options = {
  noColor: false,
  keysColor: 'magenta',
  dashColor: 'yellow',
  stringColor: 'green',
  multilineStringColor: 'cyan',
  inlineArrays: false,
  indent: 4
};

console.log(prettyjson.render(complexData, options));

view raw JSON →