What is write-json-file?
The write-json-file npm package allows you to write JSON data to a file in a simple and straightforward manner. It is useful for saving configuration files, data storage, and other scenarios where JSON data needs to be persisted to the filesystem.
What are write-json-file's main functionalities?
Write JSON to a file
This feature allows you to write a JSON object to a specified file. The example demonstrates writing a simple JSON object to a file named 'foo.json'.
const writeJsonFile = require('write-json-file');
(async () => {
await writeJsonFile('foo.json', {foo: true});
})();
Write JSON to a file with options
This feature allows you to specify options such as indentation when writing JSON to a file. The example demonstrates writing a JSON object to a file with an indentation of 2 spaces.
const writeJsonFile = require('write-json-file');
(async () => {
await writeJsonFile('foo.json', {foo: true}, {indent: 2});
})();
Write JSON to a file with replacer function
This feature allows you to use a replacer function to modify the JSON data before writing it to a file. The example demonstrates writing a JSON object to a file while excluding the 'bar' property.
const writeJsonFile = require('write-json-file');
(async () => {
await writeJsonFile('foo.json', {foo: true, bar: false}, {replacer: (key, value) => key === 'bar' ? undefined : value});
})();
Other packages similar to write-json-file
jsonfile
The jsonfile package provides similar functionality for reading and writing JSON files. It offers a simple API for both synchronous and asynchronous operations. Compared to write-json-file, jsonfile includes both read and write capabilities in a single package.
fs-extra
The fs-extra package extends the native Node.js fs module with additional methods, including methods for reading and writing JSON files. It provides a more comprehensive set of file system utilities compared to write-json-file, which focuses solely on writing JSON data.
json-write
The json-write package is another alternative for writing JSON data to files. It offers a straightforward API similar to write-json-file but with fewer configuration options. It is a simpler and more lightweight option for basic JSON writing tasks.
write-json-file
Stringify and write JSON to a file atomically
Creates directories for you as needed.
Install
npm install write-json-file
Usage
import {writeJsonFile} from 'write-json-file';
await writeJsonFile('foo.json', {foo: true});
API
writeJsonFile(filePath, data, options?)
Returns a Promise
.
writeJsonFileSync(filePath, data, options?)
options
Type: object
indent
Type: string | number | undefined
Default: '\t'
Indentation as a string or number of spaces.
Pass in undefined
for no formatting.
If you set both this and detectIndent
, this value will be used when the indentation cannot be detected.
detectIndent
Type: boolean
Default: false
Detect indentation automatically if the file exists.
sortKeys
Type: boolean | Function
Default: false
Sort the keys recursively.
Optionally pass in a compare
function.
replacer
Type: Function
Passed into JSON.stringify
.
mode
Type: number
Default: 0o666
The mode used when writing the file.
Related