Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
json-colorizer
Advanced tools
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.
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));
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 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 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.
A library for colorizing JSON strings
This package is a simple console syntax highlighter for JSON.
npm install --save json-colorizer
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
}));
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 }));
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.
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
valuecolor
objectblack
red
green
yellow
blue
magenta
cyan
white
gray
FAQs
A library to format JSON with colors for display in the console
The npm package json-colorizer receives a total of 358,740 weekly downloads. As such, json-colorizer popularity was classified as popular.
We found that json-colorizer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.