# json-assert

> check json object matches template

Latest version **1.0.2** (published 2015-03-20) · MIT license · 0 weekly downloads

## Install

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

## 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.2 |
| Published | 2015-03-20 |
| First published | 2015-03-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | James Brooks |
| Maintainers | kerspoon |
| Keywords | json, assert, check |

## Links

- npm: https://www.npmjs.com/package/json-assert
- Repository: https://github.com/5app/json-assert
- Issues: https://github.com/5app/json-assert/issues
- npm.io page: https://npm.io/package/json-assert

## Dependencies (1)

- [underscore](https://npm.io/package/underscore.md) ^1.8.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.0.2 (latest) — 2015-03-20
- 1.0.1 — 2015-03-20

## README

JSON Assert
===========

This program checks that a javascript object matches another. The main use case is for use in testing a JSON API.

The difference between this and [_.isEqual](http://underscorejs.org/#isEqual) is that json-assert accepts a function which will run that can perform addition checks. See the examples below.

The difference between this and [JSON schema](http://json-schema.org/) is that json-assert is less verbose, but it is also less flexible.


## Installation


    npm install json-assert --save


## Tests


    npm test


## Usage


    var ja = require('json-assert');

    // basic things must match
    ja.isEqual({ a: 3}, { b: 4}); // false
    ja.isEqual({ a: 3}, { a: 3}); // true

    // we don't care what the value is as long as it exists.
    ja.isEqual({ a: ja.dontCare }, { a: 3}); // true

    // it must exist and match the type (typeof)
    ja.isEqual({ a: ja.matchType('string') }, { a: 4}); // false
    ja.isEqual({ a: ja.matchType('string') }, { a: "4"}); // true

    // we don't care if it exists or not
    ja.isEqual({ a: ja.optional }, { a: 4 }); // true
    ja.isEqual({ a: ja.optional }, { }); // true


Here is a more realistic example.


    var ja = require('json-assert');
    var request = require('request');

    function testAjax(url, expected) {
      request(url, function (error, response, body) {
        assert(!error);
        assert.equal(response.statusCode, 200);
        assert(ja.isEqual(expected, JSON.parse(body)));
      });
    }

    testAjax('http://api.test.com', {
      name: "bob",
      age: 45,
      lastLogin: ja.matchType('string')
    });


## Contributing

Make sure your code:

- has been run through [js beautifier](http://jsbeautifier.org/)
- passes [js hint](http://jshint.com/)
- has tests written for all new code
- that all tests pass

Then submit a pull request.

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