# json-parse-safe

> Parse your json safely and stop writing try {} catch {}

Latest version **2.0.0** (published 2019-06-13) · ISC license · 11.4K weekly downloads

## Install

```sh
npm install json-parse-safe
pnpm add json-parse-safe
yarn add json-parse-safe
bun add json-parse-safe
```

## Health

**Score 33/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2019-06-13 |
| First published | 2015-02-04 |
| Weekly downloads | 11.4K |
| License | ISC |
| TypeScript types | separate (@types/json-parse-safe) |
| Module format | CommonJS |
| Node | >=6 |
| Dependencies | 0 |
| Unpacked size | 4.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Author | @joaquimserafim |
| Maintainers | quim |
| Keywords | json, parse, safe, try/catch |

## Links

- npm: https://www.npmjs.com/package/json-parse-safe
- Repository: https://github.com/joaquimserafim/json-parse-safe
- Issues: https://github.com/joaquimserafim/json-parse-safe/issues
- npm.io page: https://npm.io/package/json-parse-safe

## 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

- 2.0.0 (latest) — 2019-06-13
- 1.0.5 — 2017-11-14
- 1.0.3 — 2015-02-04
- 1.0.2 — 2015-02-04
- 1.0.1 — 2015-02-04
- 1.0.0 — 2015-02-04

## README

# json-parse-safe

Parse your json safely and stop writing `try {} catch {}`

<a href="https://nodei.co/npm/json-parse-safe/"><img src="https://nodei.co/npm/json-parse-safe.png?downloads=true"></a>

[![Build Status](https://img.shields.io/badge/build-passing-brightgreen.svg?style=flat-square)](https://travis-ci.org/joaquimserafim/json-parse-safe)![Code Coverage 100%](https://img.shields.io/badge/code%20coverage-100%25-green.svg?style=flat-square)[![ISC License](https://img.shields.io/badge/license-ISC-blue.svg?style=flat-square)](https://github.com/joaquimserafim/json-parse-safe/blob/master/LICENSE)

## Syntax
> var JSONParse = require('json-parse-safe') <br> JSONParse(text[, reviver])

### Parameters
##### text
*   the string to parse as JSON

##### reviver *(optional)*
*   if a function, prescribes how the value originally produced by parsing is transformed, before being returned, more info about this param in [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter)

### Returns
##### object `{value, error}` 
*   **value:** should be the **object** corresponding to the given JSON text or **undefined** in case of error
*   **error:** should be an **Error** object in case of error or **undefined** in case of success


## Example

``` js
var JSONParse = require('json-parse-safe');

var str = '{"cat": "Peter", "age": 1, "colors": ["white", "cyan", "black"]}';

var obj = JSONParse(str);
// obj.value should be
{
    cat: 'Peter',
    age: 1,
    colors: ['white', 'cyan', 'black']
}
```

**getting the exception**
``` js
var bad = '{"cat": "Peter" "age": 1}';

var obj = JSONParse(str);

// obj.value should be 'undefined'
// obj.error should be an Error
console.log(obj.error);
console.log(obj.error.message);
console.log(obj.error.stack);
```

## Development

##### This project has been set up with a precommit that forces you to follow a code style, no jshint issues and 100% of code coverage before commit



to run test
``` js
npm test
```

to run jshint
``` js
npm run jshint
```

to run code style
``` js
npm run code-style
```

to run check code coverage
``` js
npm run check-coverage
```

to open the code coverage report
``` js
npm run open-coverage
```

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