JSON.stringify's third parameter defines white-space insertion for pretty-printing. It can be a string or a number (number of spaces). Node can write to your filesystem with fs. Example:
var fs = require('fs');
fs.writeFile('test.json', JSON.stringify({ a:1, b:2, c:3 }, null, 4));
/* test.json:
{
"a": 1,
"b": 2,
"c": 3,
}
*/
See the JSON.stringify() docs at MDN, Node fs docs
Answer from Ricardo Tomasi on Stack Overflow Top answer 1 of 8
1137
JSON.stringify's third parameter defines white-space insertion for pretty-printing. It can be a string or a number (number of spaces). Node can write to your filesystem with fs. Example:
var fs = require('fs');
fs.writeFile('test.json', JSON.stringify({ a:1, b:2, c:3 }, null, 4));
/* test.json:
{
"a": 1,
"b": 2,
"c": 3,
}
*/
See the JSON.stringify() docs at MDN, Node fs docs
2 of 8
245
I think this might be useful... I love example code :)
var fs = require('fs');
var myData = {
name:'test',
version:'1.0'
}
var outputFilename = '/tmp/my.json';
fs.writeFile(outputFilename, JSON.stringify(myData, null, 4), function(err) {
if(err) {
console.log(err);
} else {
console.log("JSON saved to " + outputFilename);
}
});
npm
npmjs.com › package › @base2 › pretty-print-object
@base2/pretty-print-object - npm
April 28, 2022 - Useful for when you want to get the string representation of an object in a formatted way. It also handles circular references and lets you specify quote type. ... import { prettyPrint } from '@base2/pretty-print-object'; const obj = { foo: ...
» npm install @base2/pretty-print-object
Published Apr 28, 2022
Version 1.0.2
Author Chris Baker
Pretty-printing nested objects
This is the seminal paper on the topic https://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf But as far as I understand it, it gets a lot of power from the laziness of the implementation language. I saw a paper they does it in an eager language but can’t find it at the moment More on reddit.com
GitHub - larswaechter/voici.js: A Node.js library for pretty printing your data on the terminal
How is this better than console.table() that’s natively supported ? More on reddit.com
npm
npmjs.com › package › pretty-print-json
pretty-print-json - npm
February 27, 2026 - Pretty-print JSON data into HTML to indent and colorize (written in functional TypeScript). Latest version: 3.0.7, last published: a month ago. Start using pretty-print-json in your project by running `npm i pretty-print-json`. There are 13 ...
» npm install pretty-print-json
Minimacode
blog.minimacode.com › pretty-print-json-console-log
Pretty print a JSON object with console.log
January 27, 2022 - Use the JSON.stringify() method to print a JavaScript object on multiple lines for better readability
npm
npmjs.com › package › js-object-pretty-print
js-object-pretty-print - npm
April 5, 2017 - Serializes a javascript object to a printable string. String is formatted to be used in a pure text environments, like a console log, as an HTML output, or to create a JSON string.
» npm install js-object-pretty-print
Published Apr 05, 2017
Version 0.3.0
Author Carlos M. Vadillo
W3Schools
w3schools.io › file › json-nodejs-examples
NodeJS read,write and pretty print JSON file and object example - w3schools
var jsonData = '{ "active": true, "id": 1, "name": "john" }'; console.log(prettier.format(JSON.stringify(jsonData), { semi: false, parser: "json" }));
npm
npmjs.com › package › pretty-print
pretty-print - npm
July 31, 2018 - rightPadding - extra padding after the object key. Defaults to 2. key - name of the value to use as the list key · value - name of value to use as the list value · var print = require('pretty-print'); var options = { rightPadding: 3, key: 'name', value: 'height' }; print([ { name: 'guy', height: 'short', }, { name: 'girl', height: 'short' } ], options); // outputs: // // guy: short ·
» npm install pretty-print
Published Jul 31, 2018
Version 2.0.0
Author Scott Corgan
Repository https://github.com/scottcorgan/pretty-print
Java2s
java2s.com › example › nodejs › object › pretty-print-object.html
Pretty Print object - Node.js Object
function pp(obj) { log(prettyPrint(obj));/*from w ww. ja va 2 s . c om*/ } function prettyPrint(obj) { var toString = Object.prototype.toString, newLine = "\n", space = " ", tab = 4, buffer = "", // Second argument is indent indent = arguments[1] || 0, // For better performance, Cache indentStr for a given indent.
GitHub
github.com › Chris-Baker › pretty-print-object
GitHub - Chris-Baker/pretty-print-object: Stringify an object/array like JSON.stringify just without all the double-quotes · GitHub
import { prettyPrint } from '@base2/pretty-print-object'; const obj = { foo: 'bar', 'arr': [1, 2, 3], nested: { hello: "world" } }; const pretty = prettyPrint(obj, { indent: ' ', singleQuotes: false, inlineCharacterLimit: 12 }); console.log(pretty); /* { foo: "bar", arr: [1, 2, 3], nested: { hello: "world" } } */
Starred by 5 users
Forked by 3 users
Languages TypeScript 88.0% | JavaScript 12.0%
Research Hubs
researchhubs.com › post › computing › javascript › pretty-print-json.html
Pretty print JSON in Node.js - Research hubs
Here is a way to pretty print JSON using JSON.stringify. JSON.stringify accepts a third parameter which defines white-space insertion. It can be a string or a number (number of spaces).
npm
npmjs.com › search
pretty-print - npm search
Librarie to format js/json objects into a YAML-style colored output. Especially useful to pretty print logs.
npm
npmjs.com › package › prettyprint
prettyprint - npm
July 17, 2018 - Function to pretty-print an object with an ability to annotate every value.. Latest version: 1.1.2, last published: 8 years ago. Start using prettyprint in your project by running `npm i prettyprint`. There are 1 other projects in the npm registry ...
» npm install prettyprint
Published Jul 17, 2018
Version 1.1.2
Author Gajus Kuizinas
Repository https://github.com/gajus/prettyprint
npm
npmjs.com › package › pretty-print-object
pretty-print-object - npm
December 13, 2015 - Function to pretty-print an object with an ability to annotate every value.. Latest version: 1.1.0, last published: 9 years ago. Start using pretty-print-object in your project by running `npm i pretty-print-object`. There is 1 other project ...
» npm install pretty-print-object
Published Dec 13, 2015
Version 1.1.0
Author Gajus Kuizinas
Repository https://github.com/gajus/pretty-print-object
ReqBin
reqbin.com › code › javascript › ounkkzpp › javascript-pretty-print-json-example
How do I pretty print JSON in JavaScript?
JavaScript has a built-in JSON.stringify(obj, replacer, space) method to convert objects to JSON and pretty-print the generated JSON string.
Codemzy
codemzy.com › blog › express-pretty-print-json
Pretty print JSON responses with Express - Codemzy's Blog
June 27, 2024 - Here are some ways you can pretty print your JSON responses to the browser with Express in Node.js - either globally or for a single route, using the `space` property of `JSON.stringify()`. To send JSON with Express in Node.js, you can do this: let myObject = { name: "codemzy", likes: [ "javascript", "blogging" ], isWriting: true, }; res.json(myObject); // send as JSON response · res.json() will automatically turn your object into JSON (using JSON.stringify()), and set all the necessary headers to send your data as JSON.