Gathering detailed insights and metrics for write-json
Gathering detailed insights and metrics for write-json
Write a JSON to file disk, also creates directories in the dest path if they don't already exist.
npm install write-json
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
17,591,463
Last Day
263
Last Week
263
Last Month
108,921
Last Year
3,361,681
10 Stars
30 Commits
4 Watching
1 Branches
2 Contributors
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
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
3
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 with npm:
1$ npm install --save write-json
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'});
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);
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);
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});
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 });
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'});
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 });
Changed
Added
Fixed
JSON.stringify
receives all intended argumentsPull requests and stars are always welcome. For bugs and feature requests, please create an issue.
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
(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
You might also be interested in these projects:
Jon Schlinkert
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on January 26, 2018.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
no SAST tool detected
Details
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
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Score
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