What is color-json?
The color-json npm package is designed to enhance the readability of JSON data by adding color to the output. This is particularly useful for developers who need to quickly parse and understand JSON structures in a visually appealing way.
What are color-json's main functionalities?
Colorize JSON
This feature allows you to colorize JSON data for better readability in the console. The code sample demonstrates how to use the color-json package to print a JSON object with colorized output.
const colorJson = require('color-json');
const jsonData = {
name: "John Doe",
age: 30,
city: "New York"
};
console.log(colorJson(jsonData));
Other packages similar to color-json
json-colorizer
The json-colorizer package provides similar functionality by adding color to JSON strings for improved readability. It offers customization options for colors and styles, which can be more flexible compared to color-json.
prettyjson
Prettyjson is another package that formats and colorizes JSON data. It focuses on making JSON output more human-readable with indentation and color, similar to color-json, but also includes additional formatting options.
cli-color
Cli-color is a more general-purpose package for adding color to console output, including JSON. While it doesn't specifically target JSON, it can be used to achieve similar results by manually applying color to JSON strings.
Color JSON in the console with no dependencies

Usage
Default Usage
import cj from 'color-json';
const topPlanets = [
{
"planet": "Saturn",
"radius": {
"miles": 36184
},
"rings": true,
"moons": 53
},
{
"planet": "Earth",
"radius": {
"miles": 3959
},
"rings": null,
"moons": 1
}
];
console.log(cj(topPlanets));
Switch Colors
const customColors = {
separator: 'yellow',
string: 'yellow',
number: 'blue',
boolean: 'magenta',
null: 'red',
key: 'green'
};
console.log(cj(topPlanets, customColors));
Customize Colors
const customColorMap = {
black: '\x1b[38;2;0;0;0m',
red: '\x1b[38;2;249;133;123m',
green: '\x1b[38;2;163;238;160m',
yellow: '\x1b[38;2;209;154;102m',
blue: '\x1b[36m',
magenta: '\x1b[38;2;209;154;102m',
cyan: '\x1b[38;2;75;167;239m',
white: '\x1b[38;2;219;223;244m'
};
console.log(cj(topPlanets, undefined, customColorMap));
Custom Spacing
console.log(cj(topPlanets, undefined, undefined, 4));
Versions
3.0.x
Converted to ESM (import
instead of require
).
2.x.x
Use this version if you still need require
syntax (CommonJS).
Color Tool
hex to console color
Credits
user123444555621 for the syntax highlight regular expressions