https://github.com/b-fuze/js2json
CSS or Javascript Object Literal to JSON (No Regex)
https://github.com/b-fuze/js2json
javascript json parser
Last synced: about 2 months ago
JSON representation
CSS or Javascript Object Literal to JSON (No Regex)
- Host: GitHub
- URL: https://github.com/b-fuze/js2json
- Owner: b-fuze
- License: mit
- Created: 2018-03-18T02:35:16.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-19T18:48:41.000Z (over 8 years ago)
- Last Synced: 2025-05-29T04:43:38.412Z (about 1 year ago)
- Topics: javascript, json, parser
- Language: JavaScript
- Homepage:
- Size: 7.81 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# js2json
Will convert your CSS or your Javascript literal to JSON while attempting to preserve the majority of the original string, with a minimalistic parser (no regex)
## Usage
In JS
```js
const json = js2json(jsObjectLiteralString, stringifyValues, propertyDelimeter, transformDelimeter);
```
CLI Usage
```shell
json2json.js [-c|--css] [-s|--stringify-values]
```
Example
```shell
echo '{error: "Invalid timestamp"}' | ./js2json.js
# output: {"error": "Invalid timestamp"}
```
Example:
```js
{
str: true,
more: 10000,
"lol \" what": 10,
go: false,
"g-<": {
prop1: null,
nothing: 1
},
noMore: "props and stuff \' \\",
stuff: -1100,
propsFam: {
"depth": null,
reckless: "behaviour"
}
}
```
Becomes
```js
{
"str": true,
"more": 10000,
"lol \" what": 10,
"go": false,
"g-<": {
"prop1": null,
"nothing": 1
},
"noMore": "props and stuff \' \\",
"stuff": -1100,
"propsFam": {
"depth": null,
"reckless": "behaviour"
}
}
```
It will also be wary of (double) quoted properties; it will leave them as-is because they're already valid JSON properies.
## Why not eval(...)?
Because using `eval(...)` can potentially produce undefined behaviour in your program, not to mention security issues, you should avoid defaulting to `eval` and instead produce valid JSON to go through `JSON.parse(...)`
## Known Issues
- Assumes all quoted strings uses double quotes
- Assumes the provided literal is syntactically correct
## License
MIT