What is json-colorizer?
The json-colorizer npm package is a tool that allows you to colorize JSON strings for better readability in the terminal. It helps in debugging and logging by making JSON data more visually appealing and easier to understand.
What are json-colorizer's main functionalities?
Basic Colorization
This feature allows you to colorize a JSON string with default settings. The output will be a colorized version of the JSON string, making it easier to read in the terminal.
const colorize = require('json-colorizer');
const jsonString = '{"name": "John", "age": 30, "city": "New York"}';
console.log(colorize(jsonString));
Custom Colorization
This feature allows you to customize the colors used for different parts of the JSON string. In this example, string keys are colored cyan and string literals are colored magenta.
const colorize = require('json-colorizer');
const jsonString = '{"name": "John", "age": 30, "city": "New York"}';
const options = { colors: { STRING_KEY: 'cyan', STRING_LITERAL: 'magenta' } };
console.log(colorize(jsonString, options));
Pretty Print with Colorization
This feature allows you to pretty-print the JSON string with colorization. The output will be a well-formatted and colorized JSON string, making it even more readable.
const colorize = require('json-colorizer');
const jsonString = '{"name": "John", "age": 30, "city": "New York"}';
const options = { pretty: true };
console.log(colorize(jsonString, options));
Other packages similar to json-colorizer
chalk
Chalk is a popular npm package for styling terminal strings. While it is not specifically designed for JSON, it can be used to colorize any string, including JSON, by manually specifying the colors for different parts of the string. It offers more flexibility but requires more setup compared to json-colorizer.
cli-highlight
CLI Highlight is a package that provides syntax highlighting for code in the terminal. It supports multiple languages, including JSON. It offers more comprehensive syntax highlighting features compared to json-colorizer but may be overkill if you only need to colorize JSON.
prettyjson
PrettyJSON is a package that formats and colorizes JSON data for the terminal. It offers similar functionality to json-colorizer but focuses more on formatting and less on customization of colors.
json-colorizer
A library for colorizing JSON strings
![](https://raw.githubusercontent.com/joeattardi/json-colorizer/master/screenshot.png)
This package is a simple console syntax highlighter for JSON.
Installation
npm install --save json-colorizer
Usage
const { colorize } = require('json-colorizer');
console.log(colorize({ "foo": "bar" }));
You can also pass a JavaScript object to the colorize
function:
const { colorize } = require('json-colorizer');
console.log(colorize({
foo: 'bar',
baz: 42
}));
Pretty-printing output
By default, the output JSON will be pretty-printed with an indentation of 2 spaces. You can adjust this by passing the indent
option.
const { colorize } = require('json-colorizer');
console.log(colorize({
foo: 'bar',
baz: 42
}, { indent: 4 }));
Customizing the colors
You can override any of the colors used for token types by providing a colors
option. This should map token types to the names of color functions. These color functions are contained in the color
object exported by the library.
const { colorize, color } = require('json-colorizer');
console.log(colorize({ foo: 'bar' }, {
colors: {
StringLiteral: color.red
}
}));
The list of valid token types and color functions are listed below.
Token types
Brace
: curly braces ({
, }
)Bracket
: square brackets ([
, ]
)Colon
: colon character (:
)Comma
: comma character (,
)StringKey
: the key in a key/value pairNumberLiteral
: a number valueStringLiteral
: a string valueBooleanLiteral
: a boolean literal (true
, false
)NullLiteral
: the literal null
value
Color functions in the color
object
black
red
green
yellow
blue
magenta
cyan
white
gray