formatJsonString function is a utility for parsing and formatting JSON strings into human-readable format with proper indentation, making it easier to visualize and debug JSON data.ts
import { formatJsonString } from "@arkyn/shared";
jsonString (required)stringstringtypescript
import { formatJsonString } from "@arkyn/shared";const jsonString = '{"name":"John","age":30}';const formatted = formatJsonString(jsonString);console.log(formatted);// Output:// {// "name": "John",// "age": 30// }
typescript
import { formatJsonString } from "@arkyn/shared";try {const invalidJsonString = '{"name":"John", "age":30,';const formatted = formatJsonString(invalidJsonString);console.log(formatted);} catch (error) {console.error(error);// Output: Error: Invalid JSON string// (with additional error details)}
formatJsonObject function to format the parsed JSON data, which means it inherits all the formatting behaviors such as two-space indentation, inline display of empty objects and arrays, and proper handling of nested structures.formatJsonObject which accepts any JavaScript value, this function specifically requires a string input and will throw an error if the string is not valid JSON, ensuring strict validation of JSON data.