Installations
npm install json5-writer
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
10.16.0
NPM Version
6.13.4
Score
82.1
Supply Chain
77.8
Quality
66.9
Maintenance
50
Vulnerability
97.6
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
noahsug
Download Statistics
Total Downloads
10,955,883
Last Day
1,420
Last Week
60,189
Last Month
256,990
Last Year
3,937,025
GitHub Statistics
22 Stars
20 Commits
4 Forks
4 Watchers
1 Branches
1 Contributors
Updated on Oct 15, 2024
Bundle Size
1.18 MB
Minified
306.64 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.2.0
Package Id
json5-writer@0.2.0
Size
4.26 kB
NPM Version
6.13.4
Node Version
10.16.0
Published on
Jun 08, 2020
Total Downloads
Cumulative downloads
Total Downloads
10,955,883
Last Day
-11.4%
1,420
Compared to previous day
Last Week
1.7%
60,189
Compared to previous week
Last Month
22.9%
256,990
Compared to previous month
Last Year
-10.8%
3,937,025
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
1
json5-writer
Comment-preserving JSON / JSON5 parser
json5-writer provides an API for parsing JSON and JSON5 without losing comments or formatting. It does so by transforming JSON5 into a JavaScript AST and using jscodeshift to update values.
Example
1const json5Writer = require('json5-writer') 2const config = fs.readFileSync('config.json5', 'utf-8') 3const writer = json5Writer.load(config) 4writer.write({ 5 'eat honey': { cooldown: 3 }, 6 speak: { cooldown: 2 }, 7 bear: { actions: ['eat honey', 'speak'] }, 8}) 9fs.writeFileSync('config.json5', writer.toSource(), 'utf-8')
config.json5
diff
1 { 2 // actions 3 'eat honey': { 4- cooldown: 4, 5+ cooldown: 3, 6 }, 7+ 8+ 'speak': { 9+ cooldown: 2, 10+ }, 11 12 // Note: A day without a friend is like a pot without a single drop of honey left inside. 13 14 // entities 15 'bear': { 16- actions: [ 'eat honey' ], 17- canSpeak: true, 18+ actions: ['eat honey', 'speak'], 19 }, 20 }
Installation
1npm install --save json5-writer
Usage
1const writerInstance = json5Writer.load(jsonStr) // get a writer instance for the given JSON / JSON5 string
2writerInstance.write(objectOrArray) // update jsonStr, preserving comments
3const ast = writerInstance.ast // directly access the AST with jscodeshift API
4const newJson5 = writerInstance.toSource(options) // get the modified JSON5 string
5const newJson = writerInstance.toJSON(options) // get the modified JSON string
.write(value)
Updates the JSON / JSON5 string with the new value. Any field or property that doesn't exist in value
is removed.
To keep an existing value, use undefined
:
1const writer = json5Writer.load(`[{ name: 'Noah' }, { name: 'Nancy' }]`) 2writer.write([{ name: undefined, age: 28 }, undefined ]) 3write.toSource() // [{ name: 'Noah', age: 28 }, { name: 'Nancy' }]
.ast
Directly access the JSON5-turned-JavaScript AST, wrapped in the jscodeshift API.
1const j = require('jscodeshift') 2const writer = json5Writer.load('[1, 2, 3, 4]') 3writer.ast.find(j.Literal).forEach(path => { 4 if (path.value.value % 2 === 0) path.value.value = 0 5}) 6write.toSource() // [1, 0, 3, 0]
.toSource(options)
Get the modified JSON5 string. Alias for .toJSON5()
.
options
control what is output. By default, single quotes and trailing commas are enabled and key quote usage is inferred.
1.toSource({ quote: 'single', trailingComma: true, quoteKeys: undefined })
quoteKeys
controls whether object keys are quoted. It can have three different values:false
- no object keys will have quotestrue
- all object keys will have quotesundefined
- object key quote usage is inferred [default]
quote
can be eithersingle
ordouble
View the remaining options here.
.toJSON(options)
Same as .toSource(options)
but with quote: 'double'
, trailingComma: false
, quoteKeys: true
by default.

No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
Found 1/19 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
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
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 2 are checked with a SAST tool
Score
2.6
/10
Last Scanned on 2025-02-10
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