Registry / devops / json-string-formatter

json-string-formatter

JSON →
library1.0.1jsnpmunverified

A zero-dependency JSON string formatter that prettifies JSON strings without parsing them into objects, avoiding the overhead and potential errors of JSON.parse. Version 1.0.1 is the latest stable release; the project appears to be in early maintenance. Unlike typical formatters that rely on JSON.parse/stringify, this library works directly on the string, preserving non-standard JSON syntax like single quotes or trailing commas.

npm install json-string-formatter
INSTALL
IMPORT
SIG · JSON-STRING-FORMAT
J
json-string-formatter
devopsjavascriptv1.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

format
import { format } from 'json-string-formatter'
const format = require('json-string-formatter')
Named import via ESM is preferred. CommonJS require returns an object with format as a property.
jsonFormatter (default)
import jsf from 'json-string-formatter'
const jsf = require('json-string-formatter')
Default import works as ESM. CommonJS require gives the module.exports object directly.
require() default
const jsonFormatter = require('json-string-formatter')
CommonJS usage: module.exports is the object with format property.

Demonstrates basic formatting and custom indent/linebreak options using ES module import.

import { format } from 'json-string-formatter'; const input = "{'text': 'hello world!'}"; const output = format(input); console.log(output); /** * { * 'text': 'hello world!' * } */ const custom = format(input, '--', '|'); console.log(custom); /** * {|--'text': 'hello world!'|} */
Debug
Known issues
gotchaLibrary does not validate JSON. Invalid JSON strings may produce unexpected output or errors.
fix
Validate input with JSON.parse before formatting if standard JSON is required.
affects: >=1.0.0
gotchaSingle quotes in JSON input are not valid JSON but are accepted by this library; output preserves single quotes.
fix
Use double quotes for valid JSON; this library is intended for non-standard or relaxed JSON strings.
affects: >=1.0.0
breakingVersion 1.0.0 changed from default export to named export. Older require patterns may break.
fix
Use `const { format } = require('json-string-formatter')` or `import { format } from 'json-string-formatter'`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: jsonFormatter.format is not a function
Using default import expecting a function but module exports an object.
fix
Use named import: `import { format } from 'json-string-formatter'` or access property: `const { format } = require('json-string-formatter')`.
SyntaxError: Unexpected token ' in JSON
Input uses single quotes which are not valid JSON.
fix
Replace single quotes with double quotes for standard JSON; this library is lenient but JSON.parse will fail.
TypeError: Cannot read properties of undefined (reading 'replace')
Passing non-string input to format() (e.g., undefined or an object).
fix
Ensure input is a string before calling format().
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
json-string-formatter — npm install json-string-formatter · libregistry