https://github.com/yoannchb-pro/wtf-json
Parse any kind of broken json for scrapping easily
https://github.com/yoannchb-pro/wtf-json
ast broken-json json-parser nodejs object-parsing parser tokenizer typescript
Last synced: about 1 year ago
JSON representation
Parse any kind of broken json for scrapping easily
- Host: GitHub
- URL: https://github.com/yoannchb-pro/wtf-json
- Owner: yoannchb-pro
- License: mit
- Created: 2023-07-04T18:30:12.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-05-07T03:14:01.000Z (about 2 years ago)
- Last Synced: 2025-05-12T22:15:34.678Z (about 1 year ago)
- Topics: ast, broken-json, json-parser, nodejs, object-parsing, parser, tokenizer, typescript
- Language: TypeScript
- Homepage: https://yoannchb-pro.github.io/wtf-json/
- Size: 61.5 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# wtf-json
Parse any kind of broken json for scrapping easily.
## Goal
The primary objective is to ensure error-free parsing of JSON data. This tool enables you to parse any JSON or JavaScript object, regardless of its validity. Whether the input is a valid JSON or not, you can rely on this tool to handle it seamlessly without encountering any errors.
## Update
See the [CHANGELOG](./CHANGELOG.md)
## Install
```
$ 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
### Parse simple JSON
```js
wtfJson(
'{ "name": "Will", "age": 21, "favorite-food": ["Matcha", "Dumpling"] }'
);
/*
* Will be parse as follow:
* {
* name: "Will",
* age: 21,
* favorite-food: ["Matcha", "Dumpling"]
* }
*/
```
### Parse JS object
```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
* }
*/
```
### Parse broken JSON
```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"]
* }
*/
```
### Parse multiples broken JSON
```js
wtfJson(`null,null
,null,\\n
{ data: [{ "id": 6 }] }`);
/*
* Will be parse as follow:
* [
* null,
* null,
* null,
* "\n",
* {
* data: { id: 6 }
* }
* ]
*/
```