json-chalker
TypeScript icon, indicating that this package has built-in type declarations

2.2.4 • Public • Published

json-chalker

A library for colorizing JSON strings, based on json-colorizer.

This package is a simple console syntax highlighter for JSON.

Installation

npm install --save json-chalker

Usage

const { colorize } = require('json-chalker');
console.log(colorize({ "foo": "bar" }));

Specifying colors

You can specify a color to use for coloring individual tokens by providing a colors object in the options object. This should map token types to the names of color functions (see the chalk styles reference).

A color can also be specified as a hex value starting with the # symbol.

const { colorize } = require('json-chalker');
console.log(colorize({ "foo": "bar" }, {
  colors: {
    STRING_KEY: 'green',
    STRING_LITERAL: 'magenta.bold',
    NUMBER_LITERAL: '#FF0000'
  }
}));

The tokens available are:

  • BRACE
  • BRACKET
  • COLON
  • COMMA
  • STRING_KEY
  • STRING_LITERAL
  • NUMBER_LITERAL
  • BOOLEAN_LITERAL
  • NULL_LITERAL

Custom formatting

You may specify custom formatting like this:

const { colorize } = require('json-chalker');
console.log(colorize({ "foo": "I'm blue", "bar": "I'm red" }, {
  colors: {
    CUSTOM: (key, value, parent) => {
        if(key == "foo") return 'blue';
        if(value == "I'm red") return 'red';
    }
  }
}));

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.2.4
    2
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 2.2.4
    2
  • 2.2.3
    0
  • 2.2.2
    0

Package Sidebar

Install

npm i json-chalker

Weekly Downloads

2

Version

2.2.4

License

MIT

Unpacked Size

15.3 kB

Total Files

11

Last publish

Collaborators

  • ronpenton