Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
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 617,085 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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.