Gathering detailed insights and metrics for jsonfile
Gathering detailed insights and metrics for jsonfile
Gathering detailed insights and metrics for jsonfile
Gathering detailed insights and metrics for jsonfile
npm install jsonfile
Typescript
Module System
Node Version
NPM Version
98.2
Supply Chain
99.4
Quality
78.3
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
11,436,895,794
Last Day
3,205,838
Last Week
39,820,741
Last Month
241,511,088
Last Year
2,889,214,236
1,205 Stars
179 Commits
331 Forks
25 Watching
1 Branches
26 Contributors
Minified
Minified + Gzipped
Latest Version
6.1.0
Package Id
jsonfile@6.1.0
Size
5.68 kB
NPM Version
6.14.8
Node Version
14.10.1
Publised On
31 Oct 2020
Cumulative downloads
Total Downloads
Last day
-70.3%
3,205,838
Compared to previous day
Last week
-31.3%
39,820,741
Compared to previous week
Last month
-11.4%
241,511,088
Compared to previous month
Last year
7.1%
2,889,214,236
Compared to previous year
2
1
Easily read/write JSON files in Node.js. Note: this module cannot be used in the browser.
Writing JSON.stringify()
and then fs.writeFile()
and JSON.parse()
with fs.readFile()
enclosed in try/catch
blocks became annoying.
npm install --save jsonfile
readFile(filename, [options], callback)
readFileSync(filename, [options])
writeFile(filename, obj, [options], callback)
writeFileSync(filename, obj, [options])
options
(object
, default undefined
): Pass in any fs.readFile
options or set reviver
for a JSON reviver.
throws
(boolean
, default: true
). If JSON.parse
throws an error, pass this error to the callback.
If false
, returns null
for the object.1const jsonfile = require('jsonfile') 2const file = '/tmp/data.json' 3jsonfile.readFile(file, function (err, obj) { 4 if (err) console.error(err) 5 console.dir(obj) 6})
You can also use this method with promises. The readFile
method will return a promise if you do not pass a callback function.
1const jsonfile = require('jsonfile') 2const file = '/tmp/data.json' 3jsonfile.readFile(file) 4 .then(obj => console.dir(obj)) 5 .catch(error => console.error(error))
options
(object
, default undefined
): Pass in any fs.readFileSync
options or set reviver
for a JSON reviver.
throws
(boolean
, default: true
). If an error is encountered reading or parsing the file, throw the error. If false
, returns null
for the object.1const jsonfile = require('jsonfile') 2const file = '/tmp/data.json' 3 4console.dir(jsonfile.readFileSync(file))
options
: Pass in any fs.writeFile
options or set replacer
for a JSON replacer. Can also pass in spaces
, or override EOL
string or set finalEOL
flag as false
to not save the file with EOL
at the end.
1const jsonfile = require('jsonfile') 2 3const file = '/tmp/data.json' 4const obj = { name: 'JP' } 5 6jsonfile.writeFile(file, obj, function (err) { 7 if (err) console.error(err) 8})
Or use with promises as follows:
1const jsonfile = require('jsonfile') 2 3const file = '/tmp/data.json' 4const obj = { name: 'JP' } 5 6jsonfile.writeFile(file, obj) 7 .then(res => { 8 console.log('Write complete') 9 }) 10 .catch(error => console.error(error))
formatting with spaces:
1const jsonfile = require('jsonfile') 2 3const file = '/tmp/data.json' 4const obj = { name: 'JP' } 5 6jsonfile.writeFile(file, obj, { spaces: 2 }, function (err) { 7 if (err) console.error(err) 8})
overriding EOL:
1const jsonfile = require('jsonfile') 2 3const file = '/tmp/data.json' 4const obj = { name: 'JP' } 5 6jsonfile.writeFile(file, obj, { spaces: 2, EOL: '\r\n' }, function (err) { 7 if (err) console.error(err) 8})
disabling the EOL at the end of file:
1const jsonfile = require('jsonfile') 2 3const file = '/tmp/data.json' 4const obj = { name: 'JP' } 5 6jsonfile.writeFile(file, obj, { spaces: 2, finalEOL: false }, function (err) { 7 if (err) console.log(err) 8})
appending to an existing JSON file:
You can use fs.writeFile
option { flag: 'a' }
to achieve this.
1const jsonfile = require('jsonfile') 2 3const file = '/tmp/mayAlreadyExistedData.json' 4const obj = { name: 'JP' } 5 6jsonfile.writeFile(file, obj, { flag: 'a' }, function (err) { 7 if (err) console.error(err) 8})
options
: Pass in any fs.writeFileSync
options or set replacer
for a JSON replacer. Can also pass in spaces
, or override EOL
string or set finalEOL
flag as false
to not save the file with EOL
at the end.
1const jsonfile = require('jsonfile') 2 3const file = '/tmp/data.json' 4const obj = { name: 'JP' } 5 6jsonfile.writeFileSync(file, obj)
formatting with spaces:
1const jsonfile = require('jsonfile') 2 3const file = '/tmp/data.json' 4const obj = { name: 'JP' } 5 6jsonfile.writeFileSync(file, obj, { spaces: 2 })
overriding EOL:
1const jsonfile = require('jsonfile') 2 3const file = '/tmp/data.json' 4const obj = { name: 'JP' } 5 6jsonfile.writeFileSync(file, obj, { spaces: 2, EOL: '\r\n' })
disabling the EOL at the end of file:
1const jsonfile = require('jsonfile') 2 3const file = '/tmp/data.json' 4const obj = { name: 'JP' } 5 6jsonfile.writeFileSync(file, obj, { spaces: 2, finalEOL: false })
appending to an existing JSON file:
You can use fs.writeFileSync
option { flag: 'a' }
to achieve this.
1const jsonfile = require('jsonfile') 2 3const file = '/tmp/mayAlreadyExistedData.json' 4const obj = { name: 'JP' } 5 6jsonfile.writeFileSync(file, obj, { flag: 'a' })
(MIT License)
Copyright 2012-2016, JP Richardson jprichardson@gmail.com
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 9/24 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-12-16
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