Parse JSON

parseJSON lets you parse a JSON value without a try/catch block. At the same time, you can add a default fallback value.


Parse JSON without try/catch

Just use parseJSON and save a few lines of code each time you want to try to parse a JSON value that might be invalid.

main.js
// Before
try {
const cookie = context.cookies.get('cookieName')
const cookie = parseJSON(cookie)
} catch (error) {
console.error(error)
}
main.js
// After
const cookie = context.cookies.get('cookieName')
const cookie = parseJSON(cookie)