V
V
VicTHOR2019-11-17 18:31:12
Node.js
VicTHOR, 2019-11-17 18:31:12

Can I somehow write an object to the json file, and not a string?

fs.writeFileSync('file.json', data);
If data is JSON.stringify() , then the string is written to file.json. If you open such a file, it cannot be read without a beautifier.
If data is JSON.parse() , then '[object Object]' is written to file.json. I would like to somehow write the object itself, ala outerHTML.
Maybe use some other module?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Yurievich, 2019-11-17
@VicTHOR

Alternatively, you can use the same JSON.stringify , but with additional parameters:

fs.writeFileSync('file.json', JSON.stringify(data, null, '\t'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question