Write a JSON to file disk, also creates directories in the dest path if they don't already exist.
Installations
npm install write-json
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=4
Node Version
9.1.0
NPM Version
5.6.0
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
jonschlinkert
Download Statistics
Total Downloads
17,591,463
Last Day
263
Last Week
263
Last Month
108,921
Last Year
3,361,681
GitHub Statistics
10 Stars
30 Commits
4 Watching
1 Branches
2 Contributors
Bundle Size
2.40 kB
Minified
957.00 B
Minified + Gzipped
Package Meta Information
Latest Version
3.0.1
Package Id
write-json@3.0.1
Size
4.62 kB
NPM Version
5.6.0
Node Version
9.1.0
Publised On
26 Jan 2018
Total Downloads
Cumulative downloads
Total Downloads
17,591,463
Last day
0%
263
Compared to previous day
Last week
-98.9%
263
Compared to previous week
Last month
-21.1%
108,921
Compared to previous month
Last year
-51.3%
3,361,681
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
3
write-json
![Linux Build Status](https://img.shields.io/travis/jonschlinkert/write-json.svg?style=flat&label=Travis)
Write a JSON file to disk, also creates intermediate directories in the destination path if they don't already exist.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install
Install with npm:
1$ npm install --save write-json
Usage
1var writeJson = require('write-json'); 2 3// async 4writeJson('foo.json', {abc: 'xyz'}, function(err) { 5 // do stuff with err 6}); 7 8// sync 9writeJson.sync('foo.json', {abc: 'xyz'});
JSON.stringify
arguments
All methods support the same arguments as JSON.stringify (note that if you want to pass a replacer function to writeJson
- the main export, you must pass the replacer on an options object)
1// async 2writeJson('foo.json', {abc: 'xyz'}, null, 2, function(err) { 3 if (err) console.log(err); 4}); 5 6// sync 7writeJson.sync('foo.json', {abc: 'xyz'}, null, 2);
options
Or as an options object:
1var options = { 2 replacer: function(key, value) { 3 // filter out properties 4 if (typeof value === 'string') { 5 return undefined; 6 } 7 return value; 8 }, 9 indent: 2 10}; 11 12// async 13writeJson('foo.json', {abc: 'xyz'}, options, function(err) { 14 if (err) console.log(err); 15}); 16 17// sync 18writeJson.sync('actual/test.json', expected, options);
API
writeJson
Calls JSON.stringify
on the given value
then asynchronously writes the result to a file, replacing the file if it already exists and creating any intermediate directories if they don't already exist. Returns a promise if a callback function is not passed.
Params
filepath
{string}: Destination file pathvalue
{object}: Value to stringify.options
{object}: Options to pass to JSON.stringifycallback
{Function}: (optional) If no callback is provided, a promise is returned.returns
{undefined}
Example
1var writeJson = require('write'); 2var pkg = {name: 'write-json'}; 3 4writeJson('foo.json', pkg, function(err) { 5 if (err) console.log(err); 6}); 7 8// pass options to JSON.stringify explicitly 9writeJson('foo.json', pkg, null, 2, function(err) { 10 if (err) console.log(err); 11}); 12 13// pass options to JSON.stringify as an object 14// (since this method returns a promise if no callback is passed, 15// if you want to pass a replacer function to JSON.stringify, it 16// must be passed on an options object) 17writeJson('foo.json', pkg, { 18 indent: 2, 19 replacer: function(value) { 20 // filter out properties 21 if (typeof value === 'string') { 22 return undefined; 23 } 24 return value; 25 } 26}, function(err) { 27 if (err) console.log(err); 28});
.promise
The promise version of writeFile. Returns a promise.
Params
filepath
{String}: Destination file pathvalue
{any}: The value to stringifyoptions
{object}: Options to pass to JSON.stringifyreturns
{Promise}
Example
1var writeJson = require('write'); 2writeJson.promise('package.json', {name: 'write-json'}) 3 .then(function() { 4 // do stuff 5 });
.sync
The synchronous version of writeFile. Returns undefined.
Params
filepath
{String}: Destination file pathvalue
{any}: The value to stringifyoptions
{object}: Options to pass to JSON.stringifyreturns
{undefined}
Example
1var writeJson = require('write'); 2writeJson.sync('package.json', {name: 'write-json'});
.stream
The stream version of writeFile. Returns a new WriteStream object.
Params
filepath
{string|Buffer|integer}: filepath or file descriptor.options
{object}: Options to pass to mkdirp and [fs.createWriteStream][fs]{#fs_fs_createwritestream_path_options}returns
{Stream}: Returns a new WriteStream object. (See Writable Stream).
Example
1var fs = require('fs'); 2var writeJson = require('write'); 3fs.createReadStream('defaults.json') 4 .pipe(writeJson.stream('package.json')) 5 .on('close', function() { 6 // do stuff 7 });
Release history
v2.0.0 - 2017-07-10
Changed
- The main function now returns a promise if no callback is passed
Added
- adds promise support
- adds stream support
v1.0.0 - 2017-04-12
Fixed
- Make sure
JSON.stringify
receives all intended arguments
About
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running Tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
1$ npm install && npm test
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
1$ npm install -g verbose/verb#dev verb-generate-readme && verb
Related projects
You might also be interested in these projects:
- delete: Delete files and folders and any intermediate directories if they exist (sync and async). | homepage
- read-data: Read JSON or YAML files. | homepage
- read-yaml: Very thin wrapper around js-yaml for directly reading in YAML files. | homepage
- write-data: Write a YAML or JSON file to disk. Automatically detects the format to write based… more | homepage
- write-yaml: Write YAML. Converts JSON to YAML writes it to the specified file. | homepage
- write: Write data to a file, replacing the file if it already exists and creating any… more | homepage
Author
Jon Schlinkert
License
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on January 26, 2018.
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Score
3
/10
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More