Parse JSON
A wrapper for 'JSON.parse()' to support 'undefined' value. If parsing fails, an error is thrown.
Category | Parsers |
Author | Pol Gubau |
Since | 1.0.0 |
Last Updated | 2025-03-15 |
Type | Sync |
Compatibility | Universal |
Example
Input
parseJSON('{"name":"John"}')
Output:
{ name: 'John' }
Input
parseJSON('null')
Output:
undefined
Input
parseJSON('undefined')
Output:
Error: parsing error on value: undefined
Return
Parsed object of type T or undefined if the input is null or an error occurs.
Type: T | undefined
Props
Name | Type | Default | Required | Description |
---|---|---|---|---|
value | string | null | null | Yes | The string to be parsed as JSON. |
Notes
This function safely parses a JSON string. It returns 'undefined' for null or non-parsable values, and throws an error when parsing fails.
Errors
Thrown if parsing the value fails.
Tags
- JSON
- parse
- wrapper
- undefined
- error handling