Skip to content

parseJson()

ts
function parseJson(data): ParseResult<JsonValue>

Parses a JSON value from a JSON string.

Parameters

ParameterTypeDescription
dataunknown

Returns

ParseResult<JsonValue>

Examples

ts
parseJson('{"key": "value"}') // -> ParseSuccess<JsonValue>
parseJson('123') // -> ParseSuccess<JsonValue>

The value must be a JSON-string:

ts
parseJson('not a json string') // -> ParseFailure
parseJson(123) // -> ParseFailure
parseJson(null) // -> ParseFailure

You can chain together parseJson with other parsers:

ts
const parseUserJson = chain(parseJson, parseUser)
parseUserJson('{"name": "John", "age": 30}') // -> ParseSuccess<{ name: string, age: number }>