
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
json-format
Advanced tools
The json-format npm package is a utility for formatting JSON strings in a readable and pretty-printed manner. It allows developers to easily convert JSON objects into a human-readable format with customizable indentation and other formatting options.
Basic Formatting
This feature allows you to format a JSON object into a readable string with default settings.
const jsonFormat = require('json-format');
const jsonObject = { name: 'John', age: 30, city: 'New York' };
const formattedJson = jsonFormat(jsonObject);
console.log(formattedJson);
Custom Indentation
This feature allows you to customize the indentation of the formatted JSON string. In this example, the indentation is set to 4 spaces.
const jsonFormat = require('json-format');
const jsonObject = { name: 'John', age: 30, city: 'New York' };
const config = { type: 'space', size: 4 };
const formattedJson = jsonFormat(jsonObject, config);
console.log(formattedJson);
Customizing Key Order
This feature allows you to specify the order of keys in the formatted JSON string. In this example, the keys are ordered as 'city', 'name', and 'age'.
const jsonFormat = require('json-format');
const jsonObject = { name: 'John', age: 30, city: 'New York' };
const config = { type: 'space', size: 2, reorderKeys: ['city', 'name', 'age'] };
const formattedJson = jsonFormat(jsonObject, config);
console.log(formattedJson);
The prettyjson package provides similar functionality for formatting JSON objects into a readable string. It offers options for customizing colors and indentation. Compared to json-format, prettyjson focuses more on colorized output for better readability in the terminal.
The json-stringify-pretty-compact package is another alternative for formatting JSON strings. It aims to produce compact and pretty-printed JSON output. Unlike json-format, it focuses on minimizing the output size while maintaining readability.
The json-beautify package provides functionality to beautify JSON strings with customizable indentation and sorting options. It is similar to json-format but offers additional features like sorting keys alphabetically.
Parse JavaScript Object to a JSON String indented.
npm install json-format
var jsonFormat = require('./');
var fs = require('fs');
var obj = {
a: 1,
b: 2
}
/* using config default, indent with tabs */
fs.writeFile('example_tabs.json', jsonFormat(obj), function(err){
if (err) throw err;
console.log('saved');
});
/* using indent with spaces */
var config = {
type: 'space',
size: 2
}
fs.writeFile('example_spaces.json', jsonFormat(obj, config), function(err){
if (err) throw err;
console.log('saved');
});
example_tabs.json
{
"a": 1,
"b": 2
}
example_spaces.json
{
"a": 1,
"b": 2
}
{
tab: { size: 1 },
space: { size: 4 }
}
{
type: 'tab'
}
FAQs
JSON format for good presentation
The npm package json-format receives a total of 164,634 weekly downloads. As such, json-format popularity was classified as popular.
We found that json-format demonstrated a not healthy version release cadence and project activity because the last version was released 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
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.