Gathering detailed insights and metrics for json5-writer
Gathering detailed insights and metrics for json5-writer
Gathering detailed insights and metrics for json5-writer
Gathering detailed insights and metrics for json5-writer
npm install json5-writer
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
11,870,681
Last Day
1,905
Last Week
54,207
Last Month
248,384
Last Year
3,460,563
22 Stars
20 Commits
4 Forks
3 Watchers
1 Branches
1 Contributors
Updated on Oct 15, 2024
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
Last Day
-21.1%
1,905
Compared to previous day
Last Week
-14.5%
54,207
Compared to previous week
Last Month
-4.9%
248,384
Compared to previous month
Last Year
-25.5%
3,460,563
Compared to previous year
1
1
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.
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 }
1npm install --save json5-writer
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 either single
or double
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
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
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 2025-06-30
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