๐๏ธ @bridge/json5
Module that gives access to json parsing with the json5 npm package. View Source Code
js
import { ... } from '@bridge/json5'
โก Functions
parse
The parse
function parses a string that is in JSON format to a JavaScript object. This can be benefical over using the default JSON.parse
since this parses the json5 format which means features like comments and trailing commas will not throw errors.
- Signature:
parse(str: string)
- Returns:
any
js
const jsonStr = '{ "example": "Example Value" }'
const parsedJSON = parse(jsonStr)
console.log(parsedJSON.example)
stringify
The stringify
function converts a JavaScript object to a JSON string with the specified formatting.
- Signature:
stringify(obj: any, replacer?: ((this: any, key: string, value: any) => any), space?: space?: string | number)
- Returns:
string
js
const jsonObj = {
example: 'ExampleValue',
}
const convertedJson = stringify(jsonObj)
// When you have converted an object to a string, it can be written to a file for example
await writeFile('./test.json', convertedJson)