jsonfixerdev
v1.0.10
Published
Repair broken JSON documents
Maintainers
Readme
jsonfixerdev
Repair invalid JSON documents by fixing common syntax issues.
Website: https://jsonfixer.dev
Install
npm install jsonfixerdevUsage
ES module
import { jsonfixer } from 'jsonfixerdev'
const input = "{name: 'John'}"
const fixed = jsonfixer(input)
console.log(fixed) // '{"name": "John"}'CommonJS
const { jsonfixer } = require('jsonfixerdev')
const input = "{name: 'John'}"
const fixed = jsonfixer(input)
console.log(fixed)Streaming (Node.js)
import { createReadStream, createWriteStream } from 'node:fs'
import { pipeline } from 'node:stream'
import { jsonfixerTransform } from 'jsonfixerdev/stream'
const inputStream = createReadStream('./data/broken.json')
const outputStream = createWriteStream('./data/fixed.json')
pipeline(inputStream, jsonfixerTransform(), outputStream, (err) => {
if (err) console.error(err)
else console.log('done')
})What it fixes
jsonfixerdev can repair issues such as:
- Missing quotes around keys
- Missing commas or closing brackets
- Single quotes instead of double quotes
- Trailing commas
- Comments and JSONP wrappers
- Escaped JSON strings
- MongoDB-style wrappers like
NumberLong(2)andISODate(...) - Newline-delimited JSON (converted to a JSON array)
API
Regular API
// @throws JSONFixerError
jsonfixer(json: string): stringStreaming API
jsonfixerTransform(options?: { chunkSize?: number, bufferSize?: number }): TransformchunkSizecontrols output chunk size (default65536).bufferSizecontrols the sliding window size (default65536).
CLI
Install globally:
npm install -g jsonfixerdevUsage:
jsonfixerdev [filename] {OPTIONS}Options:
--version, -v Show application version
--help, -h Show this message
--output, -o Output file
--overwrite Overwrite the input file
--buffer Buffer size in bytes, for example 64K (default) or 1MExamples:
jsonfixerdev broken.json # Repair a file, output to console
jsonfixerdev broken.json > fixed.json # Repair a file, output to file
jsonfixerdev broken.json --output fixed.json # Repair a file, output to file
jsonfixerdev broken.json --overwrite # Repair a file, replace the file itself
cat broken.json | jsonfixerdev # Repair data from an input stream
cat broken.json | jsonfixerdev > fixed.json # Repair data from an input stream, output to fileLicense
ISC
