https://github.com/crescendolab-open/parse-json
A user-friendly wrapper for JSON.parse().
https://github.com/crescendolab-open/parse-json
javascript json typescript
Last synced: about 2 months ago
JSON representation
A user-friendly wrapper for JSON.parse().
- Host: GitHub
- URL: https://github.com/crescendolab-open/parse-json
- Owner: crescendolab-open
- License: mit
- Created: 2023-01-30T05:31:51.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-03T09:35:06.000Z (over 2 years ago)
- Last Synced: 2025-08-08T14:57:42.855Z (11 months ago)
- Topics: javascript, json, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@crescendolab/parse-json
- Size: 32.2 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @crescendolab/parse-json
A user-friendly wrapper for `JSON.parse()`.
```ts
// ✅ Returns type `JsonValue` which is stricter than `unknown`.
// ✅ Fallback specified value is returned if `input` is not a valid JSON string.
const output = parseJson(input, null);
// ^? const output: JsonValue
if (typeof output === "function") {
output;
// ^? const output: never
}
if (output === undefined) {
output;
// ^? const output: never
}
// ❌ Return with type `any`.
// ❌ Might throw an error if `input` is not a valid JSON string.
const vanillaOutput = JSON.parse(input);
// ^? const vanillaOutput: any
```
## Installation
```sh
pnpm i @crescendolab/parse-json
```
## Usage
```ts
import { parseJson } from "@crescendolab/parse-json";
const input = await loadJson();
const output = parseJson(input);
```
## Options
```ts
parseJson(input, options);
```
### `options.fallback`
- Type: `any`
Specify a fallback value to be returned if it fails to parse `input` as a JSON string.
```ts
const input = "not a valid json string";
// ✅ Get `undefined` instead of throwing an error.
const output = parseJson(input, { fallback: undefined });
```
### `options.parser`
- Type: `(input: string) => JsonValue`
Specify a custom parser function.
For example, you can use [`json5`](https://github.com/json5/json5) for comments in json or [parse-json](https://github.com/sindresorhus/parse-json) for helpful error messages.
```ts
import { parseJson } from "@crescendolab/parse-json";
import json5 from "json5";
const input = `{
// This is a comment
"foo": "bar"
}`;
// ✅ This will not throw an error.
const output = parseJson(input, {
parser: json5.parse,
});
```
## Create your own parser
You can create a parser with a custom default options where the options are the same as the `options` argument of `parseJson()`.
```ts
import { parseJson } from "@crescendolab/parse-json";
const myParser = parseJson.create(options);
const myParser = parseJson.create({ fallback: undefined });
```
## License
MIT © [Crescendo lab](https://www.cresclab.com/)