
Security News
npm Introduces minimumReleaseAge and Bulk OIDC Configuration
npm rolls out a package release cooldown and scalable trusted publishing updates as ecosystem adoption of install safeguards grows.
perfect-json
Advanced tools
Utility function to beautify JSON string...like JSON.stringify() but better
Utility function to beautify JSON string...like JSON.stringify() but better
npm install perfect-json
or
yarn add perfect-json
perfectJson(obj, options)
obj — JSON to beautify;options — optional parameters:
indent — count of indentation spaces (defaults to 2);compact — tells whether close and open brackets of object array items must be placed on the same line (defaults to true);singleLine — tells whether values of object properties must be placed on a single line, it can be of boolean type or a function returning a boolean result and being invoked for each property of an object recursively — the function receives an object argument with the following properties:
key — name of the current property (zero-based index in case of array);value — value of the current property;path — array consisting of names of all ascendant properties including the current one;items — array of references to all ascendant objects and arrays;line — stringified array or object value placed on a single line;depth — zero-based depth level (equals to path.length and items.length);indent — count of indentation spaces per level ((depth + 1) * indent results in a summary indentation on a given level).maxLineLength — places objects and arrays on a single line if resulting line's length is less than or equal to specified value;arrayMargin — characters after opening and before closing array brackets when array is placed on a single line (defaults to empty string meaning no gap: ["Javascript", "Node.js", "ES6"]);objectMargin — characters after opening and before closing object brackets when object is placed on a single line (defaults to ' ' meaning a gap: { "node": "14.0.0", "eslint": true, "babel": true, "typescript": false });split — function to split the resulting JSON into several nested JSONs, it accepts the same properties as singleLine function excepting line and should return a string placeholder to replace the current property by; JSON parts replaced by placeholders are stored in a separate object (where keys are placeholders) that can be accessed in splitResult function;splitResult — function being called at the end of transformation with an object of splitted JSONs as an argument.Just pass an object to stringify:
const perfectJson = require('perfect-json');
console.log(perfectJson({
name: 'Dmitriy',
surname: 'Pushkov',
skills: ['JavaScript', 'Node.js', 'ES6'],
env: { node: '14.0.0', eslint: true, babel: true, typescript: false }
}));
Result:
{
"name": "Dmitriy",
"surname": "Pushkov",
"skills": [
"JavaScript",
"Node.js",
"ES6"
],
"env": {
"node": "14.0.0",
"eslint": true,
"babel": true,
"typescript": false
}
}
Use compact option:
const perfectJson = require('perfect-json');
console.log(perfectJson([{
name: 'Dmitriy',
surname: 'Pushkov'
}, {
name: 'Tamara',
surname: 'Pushkova'
}], {
compact: false
}));
Result:
[
{
"name": "Dmitriy",
"surname": "Pushkov"
},
{
"name": "Tamara",
"surname": "Pushkova"
}
]
Use indent option:
const perfectJson = require('perfect-json');
console.log(perfectJson({
name: 'Dmitriy',
surname: 'Pushkov',
skills: ['JavaScript', 'Node.js', 'ES6'],
env: { node: '14.0.0', eslint: true, babel: true, typescript: false }
}, {
indent: 4
}));
Result:
{
"name": "Dmitriy",
"surname": "Pushkov",
"skills": [
"JavaScript",
"Node.js",
"ES6"
],
"env": {
"node": "14.0.0",
"eslint": true,
"babel": true,
"typescript": false
}
}
Use singleLine option:
const perfectJson = require('perfect-json');
console.log(perfectJson({
name: 'Dmitriy',
surname: 'Pushkov',
skills: ['JavaScript', 'Node.js', 'ES6'],
env: { node: '14.0.0', eslint: true, babel: true, typescript: false }
}, {
singleLine: ({ key }) => {
return ['skills', 'env'].includes(key);
}
}));
Result:
{
"name": "Dmitriy",
"surname": "Pushkov",
"skills": ["JavaScript", "Node.js", "ES6"],
"env": { "node": "14.0.0", "eslint": true, "babel": true, "typescript": false }
}
Use maxLineLength option:
const perfectJson = require('perfect-json');
const obj = {
name: 'Dmitriy',
surname: 'Pushkov',
skills: ['JavaScript', 'Node.js', 'ES6'],
env: { node: "14.0.0", eslint: true, babel: true, typescript: false }
};
console.log(perfectJson(obj, {
maxLineLength: 40
}));
console.log(perfectJson(obj, {
maxLineLength: 80
}));
Result:
{
"name": "Dmitriy",
"surname": "Pushkov",
"skills": ["JavaScript", "Node.js", "ES6"],
"env": {
"node": "14.0.0",
"eslint": true,
"babel": true,
"typescript": false
}
}
{
"name": "Dmitriy",
"surname": "Pushkov",
"skills": ["JavaScript", "Node.js", "ES6"],
"env": { "node": "14.0.0", "eslint": true, "babel": true, "typescript": false }
}
Use split and splitResult options together:
const perfectJson = require('perfect-json');
console.log(perfectJson({
name: 'Dmitriy',
surname: 'Pushkov',
skills: ['JavaScript', 'Node.js', 'ES6'],
env: { node: '14.0.0', eslint: true, babel: true, typescript: false }
}, {
split: ({ key, depth }) => {
if (depth !== 1) {
return null;
}
switch (key) {
case 'skills': return '#skills';
case 'env': return '#env';
default: return null;
}
},
splitResult: splitted => {
console.log(splitted['#skills']);
console.log(splitted['#env']);
}
}));
Result:
[
"JavaScript",
"Node.js",
"ES6"
]
{
"node": "14.0.0",
"eslint": true,
"babel": true,
"typescript": false
}
{
"name": "Dmitriy",
"surname": "Pushkov",
"skills": "#skills",
"env": "#env"
}
FAQs
Utility function to beautify JSON string...like JSON.stringify() but better
The npm package perfect-json receives a total of 17 weekly downloads. As such, perfect-json popularity was classified as not popular.
We found that perfect-json 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
npm rolls out a package release cooldown and scalable trusted publishing updates as ecosystem adoption of install safeguards grows.

Security News
AI agents are writing more code than ever, and that's creating new supply chain risks. Feross joins the Risky Business Podcast to break down what that means for open source security.

Research
/Security News
Socket uncovered four malicious NuGet packages targeting ASP.NET apps, using a typosquatted dropper and localhost proxy to steal Identity data and backdoor apps.