Stringifying JSON in JS is easy with built-in function: JSON.stringify(myObject, null, 2)
.
This works great for most cases, but sometimes you might want a bit more control over the output format.
NOTE: This PR needs to be merged before the explanation below is true: lydell/json-stringify-pretty-compact#19
https://github.com/lydell/json-stringify-pretty-compact
Turns this
{
"from": "decisions",
"where": {"product.name": {"$match": "Banana"}, "behavior.user": "veronica"},
"recommend": "product",
"goal": {"purchase": true},
"limit": 5
}
into this:
{
"from": "decisions",
"where": {
"product.name": { "$match": "Banana" },
"behavior.user": "veronica"
},
"recommend": "product",
"goal": { "purchase": true },
"limit": 5
}
It looks clean, and also inlines objects and arrays which don't have new arrays or objects inside them, and fit the given maxLength width.
const stringify = require('json-stringify-pretty-compact')
const obj = {
"from": "decisions",
"where": {"product.name": {"$match": "Banana"}, "behavior.user": "veronica"},
"recommend": "product",
"goal": {"purchase": true},
"limit": 5
}
const result = stringify(obj, { maxNesting: 1, margins: true })
console.log(result)
https://github.com/prettier/prettier
Does the same thing as the option 1 but doesn't have a similar option to maxNesting.
- https://www.npmjs.com/package/pretty-format for
util.inspect
replacement