Registry / serialization / json-alexander

json-alexander

JSON →
library0.1.13jsnpmunverified

json-alexander is a utility package designed to parse JSON strings that may be malformed or contain JavaScript object literal syntax, providing a more "forgiving" parsing experience than `JSON.parse`. It aims to fix common issues like unquoted keys, single quotes, and unbalanced structures. The current stable version is 0.1.13, indicating it's still in an early development phase. Release cadence is infrequent, typical for a niche utility. Its key differentiators include its ability to parse non-standard JSON and JavaScript object syntax, making it suitable for scenarios like CLI argument parsing where input might not be strictly valid JSON. However, its "forgiving" nature, particularly the `parseJSON` function, uses regular expressions which introduce a potential for ReDoS attacks, contrasting with the standard `JSON.parse` or more strict parsers. For security-sensitive applications, the `safeParse` function is provided, which foregoes the auto-correction in favor of returning `null` for malformed input, thus mitigating the ReDoS risk.

npm install json-alexander
INSTALL
IMPORT
SIG · JSON-ALEXANDER
J
json-alexander
serializationjavascriptv0.1.13
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

parseJSON
import { parseJSON } from 'json-alexander'
const parseJSON = require('json-alexander').parseJSON
This is the main forgiving parsing function, which uses regular expressions for autofixing malformed JSON. Exercise caution when processing untrusted input due to ReDoS risk.
safeParse
import { safeParse } from 'json-alexander'
const safeParse = require('json-alexander').safeParse
Recommended for server-side use or when processing untrusted input. It returns `null` for malformed JSON instead of attempting to fix it, mitigating ReDoS vulnerabilities.

Demonstrates both `parseJSON` for forgiving parsing of malformed and JS-like strings, and `safeParse` for secure, strict parsing that returns null on invalid input.

import { parseJSON, safeParse } from 'json-alexander'; console.log('--- Using parseJSON (forgiving) ---'); // Normal Valid JSON console.log('Valid JSON:', parseJSON('{"valid": "works"}')); // Javascript objects console.log('JS Object:', parseJSON({ key: 'val' })); // Malformed JSON (single quotes) console.log('Malformed (single quotes):', parseJSON("{'malformed': 'works'}")); // Unbalanced JSON console.log('Unbalanced:', parseJSON('{"unbalanced": "object"')); // Javascript objects missing quotes console.log('Missing quotes:', parseJSON('{ hello: there }')); console.log('\n--- Using safeParse (secure, non-forgiving) ---'); // Normal Valid JSON console.log('Safe Valid JSON:', safeParse('{"valid": "works"}')); // Javascript objects console.log('Safe JS Object:', safeParse({ key: 'val' })); // Malformed JSON (returns null with safeParse) console.log('Safe Malformed (returns null):', safeParse("{'malformed': 'works'}"));
Debug
Known issues
breakingThe `parseJSON` function utilizes regular expressions to correct malformed JSON, which can introduce a ReDoS (Regular Expression Denial of Service) vulnerability if highly crafted malicious input is provided. While patterns have been tested, it's an inherent risk.
fix
For server-side applications or any context processing untrusted input, *always* use the `safeParse` function, which explicitly avoids regex-based corrections and returns `null` for malformed input. Alternatively, enforce strict input validation before parsing.
affects: >=0.1.0
gotchaThe `parseJSON` function attempts to "fix" malformed JSON by guessing, which might lead to unexpected parsing results if the input is severely malformed or not intended to be fixed. It prioritizes usability over strict adherence to the JSON specification.
fix
Review the output carefully when parsing non-standard input. If strict JSON adherence is required, use `JSON.parse` or the `safeParse` function and handle parsing errors explicitly.
affects: >=0.1.0
gotchaUnlike `JSON.parse`, `parseJSON` can accept plain JavaScript objects as input and return them directly without serialization/deserialization. While convenient, this might mask issues where a string was *expected* but an object was accidentally passed.
fix
Always ensure the type of input is as expected before calling `parseJSON` if strict string-only parsing is desired. Use `typeof input === 'string'` check to validate.
affects: >=0.1.0
Errors
Common errors & fixes
Cannot read properties of undefined (reading 'key')
`safeParse` returned `null` because the input JSON was malformed, and subsequent code attempted to access properties on the `null` result.
fix
Always check if the result of `safeParse` is `null` before attempting to access its properties, and handle the invalid input case:
```javascript
const data = safeParse(userInput);
if (data === null) {
  console.error('Invalid JSON input, could not parse securely.');
  // Handle error, return default, etc.
} else {
  console.log(data.someKey);
}
```
SyntaxError: Unexpected token ' (or unexpected character)
Attempting to use the native `JSON.parse` function with malformed JSON or JavaScript object literal syntax that `json-alexander` is designed to handle.
fix
Replace `JSON.parse` with `parseJSON` from `json-alexander` if you intend to parse forgivingly, or with `safeParse` if you need the security-conscious alternative for potentially malformed input.
Upgrade
Version history
0.1.13latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
json-alexander — npm install json-alexander · libregistry