Is there a module which can pretty print JSON format data in Nodejs?

发布于 1周前 作者 h691938207 来自 nodejs/Nestjs

Is there a module which can pretty print JSON format data in Nodejs?

As the title describe,

Can you help me recomend a module?

thanks!

13 回复

yes, a module with thousands of dependency


JSON.stringify(o, null, 2)

楼上+1,原生支持

JSON.stringify or require('util').inspect

Your English is so good, why not search that by yourself?

I just put your title (as it is) into google, just 1 second or less later, the first item of the result is what you want.

英语不好真的不用硬来,本来就是中文社区。

原来如此,推荐你一个好方案!在 chrome 里安装 google 输入法插件,在 firefox 里搜一下 pinyin 之类的关键词也有插件的,我就是这样用,系统级别的输入法我就不装了,因为在 linux 里我基本上都是在网页里才需要中文输入法。

http://jsbeautifier.org/
https://www.npmjs.com/package/js-beautify


CLI Options:
-f, --file Input file(s) (Pass ‘-’ for stdin)
-r, --replace Write output in-place, replacing input
-o, --outfile Write output to file (default stdout)
--config Path to config file
--type [js|css|html] [“js”]
-q, --quiet Suppress logging to stdout
-h, --help Show this help
-v, --version Show the version

Beautifier Options:
-s, --indent-size Indentation size [4]
-c, --indent-char Indentation character [" "]
-t, --indent-with-tabs Indent with tabs, overrides -s and -c
-e, --eol Character(s) to use as line terminators.
[first newline in file, otherwise "\n]
-n, --end-with-newline End output with newline
--editorconfig Use EditorConfig to set up the options
-l, --indent-level Initial indentation level [0]
-p, --preserve-newlines Preserve line-breaks (–no-preserve-newlines disables)
-m, --max-preserve-newlines Number of line-breaks to be preserved in one chunk [10]
-P, --space-in-paren Add padding spaces within paren, ie. f( a, b )
-E, --space-in-empty-paren Add a single space inside empty paren, ie. f( )
-j, --jslint-happy Enable jslint-stricter mode
-a, --space-after-anon-function Add a space before an anonymous function’s parens, ie. function ()
-b, --brace-style [collapse|expand|end-expand|none][,preserve-inline] [collapse,preserve-inline]
-u, --unindent-chained-methods Don’t indent chained method calls
-B, --break-chained-methods Break chained method calls across subsequent lines
-k, --keep-array-indentation Preserve array indentation
-x, --unescape-strings Decode printable characters encoded in xNN notation
-w, --wrap-line-length Wrap lines at next opportunity after N characters [0]
-X, --e4x Pass E4X xml literals through untouched
--good-stuff Warm the cockles of Crockford’s heart
-C, --comma-first Put commas at the beginning of new line instead of end
-O, --operator-position Set operator position (before-newline|after-newline|preserve-newline) [before-newline]

haha,interesting

这英文太尬了。。。

Certainly! In Node.js, there’s a built-in module called util which provides a utility function named inspect that can be used to pretty-print JSON format data. Here’s how you can use it:

const util = require('util');

// Sample JSON data
const jsonData = {
    name: "John Doe",
    age: 30,
    skills: ["JavaScript", "Node.js", "React"],
    address: {
        city: "New York",
        zipcode: "10001"
    }
};

// Using util.inspect to pretty print JSON
const prettyJson = util.inspect(jsonData, { showHidden: false, depth: null, colors: true });

console.log(prettyJson);

In this example:

  • We import the util module.
  • We define a sample JSON object jsonData.
  • We use util.inspect to convert the JSON object into a pretty-printed string. The options { showHidden: false, depth: null, colors: true } are passed to control the output:
    • showHidden: false hides non-enumerable properties.
    • depth: null means no limit on recursion depth.
    • colors: true enables ANSI color styles in the output for better readability (supported in most modern terminals).
  • Finally, we print the pretty-printed JSON string to the console.

Alternatively, if you just want to pretty-print JSON for debugging purposes, you can use JSON.stringify with proper indentation:

console.log(JSON.stringify(jsonData, null, 2));

This will also format the JSON data nicely with a 2-space indentation.

回到顶部