How can I pretty-print JSON using node.js?

前端 未结 6 1066
心在旅途
心在旅途 2020-11-28 00:22

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

6条回答
  •  情书的邮戳
    2020-11-28 00:53

    Another workaround would be to make use of prettier to format the JSON. The example below is using 'json' parser but it could also use 'json5', see list of valid parsers.

    const prettier = require("prettier");
    console.log(prettier.format(JSON.stringify(object),{ semi: false, parser: "json" }));
    

提交回复
热议问题