Parse JSON

A wrapper for 'JSON.parse()' to support 'undefined' value. If parsing fails, an error is thrown.

CategoryParsers
AuthorPol Gubau
Since1.0.0
Last Updated2025-03-15
TypeSync
CompatibilityUniversal

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

NameTypeDefaultRequiredDescription
valuestring | nullnullYesThe 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

Related