Writes a JSON object to a file.
- Use
fs.writeFileSync()
, template literals andJSON.stringify()
to write ajson
object to a.json
file.
代码实现
import { writeFileSync } from 'fs';
const JSONToFile = (obj, filename) =>
writeFileSync(`${filename}.json`, JSON.stringify(obj, null, 2));
JSONToFile({ test: 'is passed' }, 'testJsonFile');
// writes the object to 'testJsonFile.json'