parseJSON(str: string)

Requires:
@ray-js/panel-sdk≥1.0.0

Description

Safely parse a JSON string into an object

Parameters

ParameterTypeRequiredDescription
strstringYesJSON string to parse

Return Value

Type: string | Record<string, never>
The parsed object; if parsing fails, returns the original string; if the input is undefined, returns an empty object

Examples

Basic usage

import { utils } from '@ray-js/panel-sdk';

const { parseJSON } = utils;

parseJSON('{"a":1}');  // { a: 1 }
parseJSON('not json'); // 'not json'
parseJSON(undefined);  // {}

stringifyJSON(data: any)

Requires:
@ray-js/panel-sdk≥1.0.0

Description

Safely serialize the given data to a JSON string

Parameters

ParameterTypeRequiredDescription
dataanyYesData to serialize; can be of any type

Return Value

Type: string
JSON string representation; if the input is null or undefined, returns an empty string; if the input is already a string, returns it as-is

Examples

Basic usage

import { utils } from '@ray-js/panel-sdk';

const { stringifyJSON } = utils;

stringifyJSON({ a: 1 }); // '{"a":1}'
stringifyJSON('hello');  // 'hello'
stringifyJSON(null);     // ''
stringifyJSON(undefined); // ''