# js-safe-json

> handle json data with safe & easy way

Latest version **1.0.9** (published 2023-02-21) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.9 |
| Published | 2023-02-21 |
| First published | 2019-04-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | ohcrider |
| Maintainers | fewspider |
| Keywords | json |

## Links

- npm: https://www.npmjs.com/package/js-safe-json
- Repository: https://github.com/ohcrider/JsSafeJson
- Homepage: https://github.com/ohcrider/JsSafeJson#readme
- Issues: https://github.com/ohcrider/JsSafeJson/issues
- npm.io page: https://npm.io/package/js-safe-json

## 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.0.9 (latest) — 2023-02-21
- 1.0.8 — 2021-09-29
- 1.0.7 — 2021-09-29
- 1.0.6 — 2021-09-29
- 1.0.5 — 2021-09-29
- 1.0.4 — 2019-04-25
- 1.0.3 — 2019-04-25
- 1.0.2 — 2019-04-25
- 1.0.1 — 2019-04-25
- 1.0.0 — 2019-04-24

## README

[English](./README.md)|[中文简体](./README.zh-CN.md)

### about
handle json data with safe & easy way

### why
Modern web frameworks, such as React and Vue, mostly render based on JSON data.

Ideally, as long as the back-end and front-end render web pages according to the agreed JSON data type, there will be no errors.
However, in the real world, in the mode of agile development, or because of later business adjustments, or changes in personnel, some data types change. At this time, new risks are introduced, such as a white screen and a webpage crash. At this time, if you spend a lot of time and energy to troubleshoot this type of error, it is not conducive to business adjustment and bug follow-up.

It is better to deal with it at the beginning, so as to avoid the problem that the web page crashes and cannot be rendered because of the change of JSON data type.

### install
```
npm install js-safe-json
```

### dev
```
npm run start
```

### run test
```
npm run test
```

### usage
```
import {
  fetchObject,
  fetchBoolean,
  fetchString,
  fetchNumber,
  fetchArray } from 'js-safe-json';

// your json data
let jsonData = {
  a: [
    {
      a1: '',
      a2: true,
      a3: false,
    },
    {
      a1: [1, 2, 3, 4],
      a2: true,
      a3: false,
    },
  ],
  b: {
    b1: 12,
    b2: 12.0,
    b3: 12.01,
    b4: 12.001
  },
  c: 0,
  d: null,
  e: '0'
};

// it is safe,if data not expect value,it will return default value
// object  default value: {}
// boolean default value: false
// string  default value: ''
// number  default value: 0
// array   default value: []

let object = fetchObject(jsonData, 'b');
let boolean = fetchBoolean(jsonData, 'b');
let string = fetchString(jsonData, 'e');
let number = fetchNumber(jsonData, 'b', 'b1');
let array = fetchArray(jsonData, 'a');
```

### License
Copyright (C) 2019 fewspider

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

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