Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bent10/json-loose
Transforms loosely structured plain object strings into valid JSON strings.
https://github.com/bent10/json-loose
json jsonify loose object plain-object stringify
Last synced: 8 days ago
JSON representation
Transforms loosely structured plain object strings into valid JSON strings.
- Host: GitHub
- URL: https://github.com/bent10/json-loose
- Owner: bent10
- License: mit
- Created: 2023-10-13T07:18:13.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-22T07:21:50.000Z (about 2 months ago)
- Last Synced: 2024-10-25T10:17:47.233Z (13 days ago)
- Topics: json, jsonify, loose, object, plain-object, stringify
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/json-loose
- Size: 124 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.md
- License: license
Awesome Lists containing this project
README
# json-loose
A utility that converts loosely structured data into valid JSON strings, ensuring data consistency and compatibility. It provides a safer alternative to using the `Function` constructor to create objects from strings.
## Install
You can install this module using npm or yarn, it's only `2.93 kB │ gzip: 1.32 kB`:
```bash
npm i json-loose
# or
yarn add json-loose
```Alternatively, you can also include this module directly in your HTML file from [CDN](https://www.jsdelivr.com/package/npm/json-loose?tab=files&path=dist):
| Type | URL |
| :--- | :---------------------------------------------------------- |
| ESM | `https://cdn.jsdelivr.net/npm/json-loose/+esm` |
| CJS | `https://cdn.jsdelivr.net/npm/json-loose/dist/index.cjs` |
| UMD | `https://cdn.jsdelivr.net/npm/json-loose/dist/index.umd.js` |## Usage
The `jsonLoose` function takes an invalid JSON string as input and returns a JSON-like string representation of the transformed data.
```js
import jsonLoose from 'json-loose'const invalidJSON = `
{
name: 'Bambang Ekalaya',
username: "@palgunadi",
age: 30,
isStudent: true
}
`
const data = jsonLoose(invalidJSON)
// now you can `JSON.parse(data)`console.log(data)
```Yields:
```json
{
"name": "Bambang Ekalaya",
"username": "@palgunadi",
"age": 30,
"isStudent": true
}
```You can also specify an optional context object to transform Identifier values:
```js
import jsonLoose from 'json-loose'const invalidJSON = `
[
"foo",
true,
[1, 2, wife],
{
[n]: 'Bambang Ekalaya',
username: "@palgunadi",
age: 30,
address: [{city:city}, [country]],
skills: skills,
isStudent: true,
relation: {
wife: wife,
guru: guru,
[bar]: "qux"
},
},
],
`const context = {
n: 'name',
skills: 'Archery',
city: 'Nishada',
country: 'Aravalli',
wife: 'Anggraini',
guru: 'Drona'
}const data = jsonLoose(invalidJSON, context)
// now you can `JSON.parse(data)`console.log(data)
```Yields:
```json
[
"foo",
true,
[1, 2, "Anggraini"],
{
"name": "Bambang Ekalaya",
"username": "@palgunadi",
"age": 30,
"address": [{ "city": "Nishada" }, ["Aravalli"]],
"skills": "Archery",
"isStudent": true,
"relation": { "wife": "Anggraini", "guru": "Drona", "bar": "qux" }
}
]
```## Related
- [attributes-parser](https://github.com/bent10/attributes-parser) – A utility for parsing and tokenizing attributes string into meaningful tokens and key-value pairs.
## Contributing
We 💛 issues.
When committing, please conform to [the semantic-release commit standards](https://www.conventionalcommits.org/). Please install `commitizen` and the adapter globally, if you have not already.
```bash
npm i -g commitizen cz-conventional-changelog
```Now you can use `git cz` or just `cz` instead of `git commit` when committing. You can also use `git-cz`, which is an alias for `cz`.
```bash
git add . && git cz
```## License
![GitHub](https://img.shields.io/github/license/bent10/json-loose)
A project by [Stilearning](https://stilearning.com) © 2023.