Skip to content

json

Utility functions for parsing JSON text and reading JSON data from external sources.

It exposes the following error codes:

Error codeDescription
UNSUPPORTED_SOURCEThe source type is not supported
READ_FAILEDReading the source failed
HTTP_ERRORAn HTTP request returned an unsuccessful response
PARSE_FAILEDJSON parsing failed

parse

Signature

js
json.parse(text, options = {})

Description

Parses a JSON string and returns the resulting JavaScript value.

Parameters

NameTypeRequiredDescription
textstringyesJSON text to parse
optionsobjectnoParsing options
options.reviverfunctionnoFunction passed to JSON.parse to transform parsed values

Returns

TypeDescription
*The parsed JSON value

Throws

Throws a TypeError if text is not a string or if options does not conform to the expected schema.

Throws an error with code json.ERROR_CODES.PARSE_FAILED if the JSON cannot be parsed. The original SyntaxError is available through the cause property.

Examples

js
import { json } from '@kalisio/common-core'

const data = json.parse('{"name":"station-01"}')
// { name: 'station-01' }

read

Signature

js
json.read(input, options = {})

Description

Reads an external source as text, then parses the resulting content as JSON.

A source can be a local file path in Node.js, a URL provided as a string or URL instance, or a Blob/File when supported by the runtime.

Parameters

NameTypeRequiredDescription
inputstring | URL | Blob | FileyesSource containing JSON data
optionsobjectnoReading and parsing options
options.encodingstringnoCharacter encoding used when reading a local file in Node.js. Defaults to 'utf-8'
options.reviverfunctionnoFunction passed to JSON.parse to transform parsed values

Returns

TypeDescription
Promise<*>The parsed JSON value

Throws

Throws an error with code json.ERROR_CODES.UNSUPPORTED_SOURCE if the source type is not supported.

Throws an error with code json.ERROR_CODES.READ_FAILED if the source cannot be read. The original error is available through the cause property.

For failed HTTP responses, the cause has code json.ERROR_CODES.HTTP_ERROR and exposes the HTTP status and statusText.

Also propagates errors thrown by json.parse.

Examples

js
import { json } from '@kalisio/common-core'

const data = await json.read('./data.json')