# json-loose

> Transforms loosely structured plain object strings into valid JSON strings

Latest version **1.2.4** (published 2024-08-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-loose
pnpm add json-loose
yarn add json-loose
bun add json-loose
```

## Health

**Score 45/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.2.4 |
| Published | 2024-08-27 |
| First published | 2023-10-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 12.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Beni Arisandi |
| Maintainers | bent10 |
| Keywords | json, jsonify, loose, object, plain-object, stringify |

## Links

- npm: https://www.npmjs.com/package/json-loose
- Repository: https://github.com/bent10/json-loose
- Homepage: https://github.com/bent10/json-loose#readme
- Issues: https://github.com/bent10/json-loose/issues
- npm.io page: https://npm.io/package/json-loose

## Dependencies (1)

- [moo](https://npm.io/package/moo.md) ^0.5.2

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.2.4 (latest) — 2024-08-27
- 1.2.3 — 2023-12-10
- 1.2.2 — 2023-10-20
- 1.2.1 — 2023-10-15
- 1.2.0 — 2023-10-15
- 1.1.0 — 2023-10-13
- 1.0.0 — 2023-10-13

## 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 💛&nbsp; 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) &copy; 2023.

---
_Source: https://npm.io/package/json-loose · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
