The Wayback Machine - https://web.archive.org/web/20190821001349/https://github.com/paularmstrong/normalizr
Skip to content
Normalizes nested JSON according to a schema
JavaScript TypeScript
Branch: master
Clone or download
dependabot and ntucker Bump lodash from 4.17.5 to 4.17.15 (#409)
Bumps [lodash](https://github.com/lodash/lodash) from 4.17.5 to 4.17.15.
- [Release notes](https://github.com/lodash/lodash/releases)
- [Commits](lodash/lodash@4.17.5...4.17.15)

Signed-off-by: dependabot[bot] <support@github.com>
Latest commit 8d03c8c Aug 6, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github [chore] update bug template with codesandbox link Jun 6, 2019
docs [chore] remove broken readme links May 29, 2019
examples [chore] Bump is-my-json-valid from 2.15.0 to 2.20.0 in /examples/redux ( Jun 19, 2019
src Fix schema key is not in entities (#380) Aug 5, 2019
typescript-tests [chore] resolve typecheck & test conflicts May 29, 2019
.babelrc.js [minor] upgrade to babel 7 May 29, 2019
.eslintignore 100% test coverage Dec 27, 2016
.eslintrc.js [chore] update eslint May 29, 2019
.flowconfig Add flow Dec 19, 2016
.gitignore [chore] update eslint May 29, 2019
.prettierrc Add prettier, lint-staged, etc Feb 8, 2018
.travis.yml [chore] resolve typecheck & test conflicts May 29, 2019
CHANGELOG.md v3.4.0 May 29, 2019
CONTRIBUTING.md add contributing guidelines for issues Jan 2, 2017
LICENSE Add Dan back to LICENSE Dec 19, 2016
README.md docs(readme): add Greenkeeper badge May 29, 2019
greenkeeper.json chore: add Greenkeeper config file May 29, 2019
husky.config.js [chore] remove typescript-definition-tester, use tsc directly May 29, 2019
index.d.ts [enhance] Improve typescript types with generics (#365) Aug 5, 2019
jest.config.js [chore] resolve typecheck & test conflicts May 29, 2019
lint-staged.config.js [chore] add md prettier to lint-staged May 29, 2019
package.json v3.4.1 Aug 5, 2019
rollup.config.js [chore] update rollup May 29, 2019
yarn.lock Bump lodash from 4.17.5 to 4.17.15 (#409) Aug 5, 2019

README.md

normalizr build status Coverage Status npm version npm downloads Greenkeeper badge

Install

Install from the NPM repository using yarn or npm:

yarn add normalizr
npm install normalizr

Motivation

Many APIs, public or not, return JSON data that has deeply nested objects. Using data in this kind of structure is often very difficult for JavaScript applications, especially those using Flux or Redux.

Solution

Normalizr is a small, but powerful utility for taking JSON with a schema definition and returning nested entities with their IDs, gathered in dictionaries.

Documentation

Examples

Quick Start

Consider a typical blog post. The API response for a single post might look something like this:

{
  "id": "123",
  "author": {
    "id": "1",
    "name": "Paul"
  },
  "title": "My awesome blog post",
  "comments": [
    {
      "id": "324",
      "commenter": {
        "id": "2",
        "name": "Nicole"
      }
    }
  ]
}

We have two nested entity types within our article: users and comments. Using various schema, we can normalize all three entity types down:

import { normalize, schema } from 'normalizr';

// Define a users schema
const user = new schema.Entity('users');

// Define your comments schema
const comment = new schema.Entity('comments', {
  commenter: user
});

// Define your article
const article = new schema.Entity('articles', {
  author: user,
  comments: [comment]
});

const normalizedData = normalize(originalData, article);

Now, normalizedData will be:

{
  result: "123",
  entities: {
    "articles": {
      "123": {
        id: "123",
        author: "1",
        title: "My awesome blog post",
        comments: [ "324" ]
      }
    },
    "users": {
      "1": { "id": "1", "name": "Paul" },
      "2": { "id": "2", "name": "Nicole" }
    },
    "comments": {
      "324": { id: "324", "commenter": "2" }
    }
  }
}

Dependencies

None.

Credits

Normalizr was originally created by Dan Abramov and inspired by a conversation with Jing Chen. Since v3, it was completely rewritten and maintained by Paul Armstrong. It has also received much help, enthusiasm, and contributions from community members.

You can’t perform that action at this time.