如何使用node.js漂亮地打印JSON ?

时间:2020-12-26 21:44:07

This seems like a solved problem but I am unable to find a solution for it.

这似乎是一个解决了的问题,但我找不到解决的办法。

Basically, I read a JSON file, change a key, and write back the new JSON to the same file. All works, but I loose the JSON formatting.So, instead of:

基本上,我读取一个JSON文件,更改一个键,并将新的JSON写回相同的文件。所有这些都可以,但是我松了JSON格式。因此,而不是:

{
  name:'test',
  version:'1.0'
}

I get

我得到

{name:'test',version:'1.1'}

Is there a way in Node.js to write well formatted JSON to file ?

节点里有办法吗?要写格式良好的JSON到文件?

3 个解决方案

#1


589  

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:

JSON。stringify的第三个参数定义了用于漂亮打印的空格插入。它可以是字符串或数字(空格数)。节点可以用fs写入文件系统。例子:

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

参见MDN .stringify()文档,节点fs文档

#2


199  

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);
    }
}); 

#3


43  

If you just want to pretty print an object and not export it as valid JSON you can use console.dir().

如果您只想漂亮地打印对象,而不将其导出为有效的JSON,那么可以使用console.dir()。

It uses syntax-highlighting, smart indentation, removes quotes from keys and just makes the output as pretty as it gets.

它使用语法突出显示、智能缩进、从键中删除引号并使输出尽可能漂亮。

const jsonString = `{"name":"John","color":"green",
                     "smoker":false,"id":7,"city":"Berlin"}`
const object = JSON.parse(jsonString)

console.dir(object, {depth: null, colors: true})

如何使用node.js漂亮地打印JSON ?

Under the hood it is a shortcut for console.log(util.inspect(…)). The only difference is that it bypasses any custom inspect() function defined on an object.

在引擎盖下面是console.log(util.inspect(…))的捷径。惟一的区别是它绕过了在对象上定义的任何自定义inspect()函数。

#1


589  

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:

JSON。stringify的第三个参数定义了用于漂亮打印的空格插入。它可以是字符串或数字(空格数)。节点可以用fs写入文件系统。例子:

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

参见MDN .stringify()文档,节点fs文档

#2


199  

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);
    }
}); 

#3


43  

If you just want to pretty print an object and not export it as valid JSON you can use console.dir().

如果您只想漂亮地打印对象,而不将其导出为有效的JSON,那么可以使用console.dir()。

It uses syntax-highlighting, smart indentation, removes quotes from keys and just makes the output as pretty as it gets.

它使用语法突出显示、智能缩进、从键中删除引号并使输出尽可能漂亮。

const jsonString = `{"name":"John","color":"green",
                     "smoker":false,"id":7,"city":"Berlin"}`
const object = JSON.parse(jsonString)

console.dir(object, {depth: null, colors: true})

如何使用node.js漂亮地打印JSON ?

Under the hood it is a shortcut for console.log(util.inspect(…)). The only difference is that it bypasses any custom inspect() function defined on an object.

在引擎盖下面是console.log(util.inspect(…))的捷径。惟一的区别是它绕过了在对象上定义的任何自定义inspect()函数。