formatJsonString function parses a JSON string and formats it into a more readable representation, using the formatJsonObject function internally.ts
import { formatJsonString } from "@arkyn/shared";
jsonStringstringstring with the formatted JSON.Error("Invalid JSON string ..."): If the input string is not valid JSON. The original parsing error will be included in the message.javascript
import { formatJsonString } from "./formatJsonString";const jsonString = '{"name":"John","age":30,"hobbies":["reading","games"]}';const formatted = formatJsonString(jsonString);console.log(formatted);/*Output:{"name": "John","age": 30,"hobbies": ["reading","games"]}*/
javascript
import { formatJsonString } from "./formatJsonString";const invalidJsonString = '{"name":"John", "age":30,';try {const formatted = formatJsonString(invalidJsonString);console.log(formatted);} catch (e) {console.error(e.message);// Expected output: "Invalid JSON string ..." followed by the syntax error.}