Save Parse Json

A wrapper for 'JSON.parse()' to support 'undefined' value. If parsing fails, it silently returns 'undefined'.

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

Example

  • Input

    saveParseJson('{"name":"John"}')

    Output:

    {"name": "John"}
  • Input

    saveParseJson('null')

    Output:

    undefined
  • Input

    saveParseJson('undefined')

    Output:

    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 parses a JSON string and returns 'undefined' for null or non-parsable values, without throwing errors.

Errors

No error is thrown, it silently returns 'undefined' in case of failure.

Tags

  • JSON
  • parse
  • wrapper
  • undefined
  • error handling

Related