Parse any kind of broken json for scrapping easily
npm install @yoannchb/wtf-json
$ npm i @yoannchb/wtf-json
`
Or with the CDN
`html
`
Import
Only for nodejs and module script
`js
import wtfJson from "wtf-json";
//or
const wtfJson = require("wtf-json");
`
Type
By default wtfJson return an any type but you can use generic type
`ts
wtfJson<{ id: number }>("{ id: 6 }").id; //autocompletion work and id is a number :)
`
Examples of use
$3
`js
wtfJson(
'{ "name": "Will", "age": 21, "favorite-food": ["Matcha", "Dumpling"] }'
);
/*
* Will be parse as follow:
* {
* name: "Will",
* age: 21,
* favorite-food: ["Matcha", "Dumpling"]
* }
*/
`
$3
`js
wtfJson(
'{ note: 20, coefficient: .5, student: "Lili", comments: [{ by: "Teacher 1", comment: "Good Job!" }], courses: undefined }'
);
/*
* Will be parse as follow:
* {
* note: 20,
* coefficient: 0.5,
* student: "Lili"
* comments: [{ by: "Teacher 1", comment: "Good Job!" }],
* courses: undefined
* }
*/
`
$3
`js
wtfJson(
'{ name Yoann, :"isAdmin":: true,, address: { country: CA }, null, {}, "roles": [::,,\'admin\' client, :user] }'
);
/*
* Will be parse as follow:
* {
* name: "Yoann",
* isAdmin: true,
* address: {
* country: "CA"
* },
* roles: ["admin", "client", "user"]
* }
*/
`
$3
`js
wtfJson(null,null
);
/*
* Will be parse as follow:
* [
* null,
* null,
* null,
* "\n",
* {
* data: { id: 6 }
* }
* ]
*/
``