Skip to content
Normalizes nested JSON according to a schema
JavaScript TypeScript
Branch: master
Clone or download

Latest commit

aimergenge and paularmstrong docs: fix example of fallbackStrategy (#439)
* fix example of fallbackStrategy

* remove `toEntity` helper for less confusion

Co-authored-by: Paul Armstrong <paularmstrong@users.noreply.github.com>
Latest commit db5b4b0 Apr 18, 2020

Files

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 docs: fix example of fallbackStrategy (#439) Apr 18, 2020
examples Bump lodash from 4.17.3 to 4.17.15 in /examples/redux (#402) Sep 3, 2019
src added a fallback strategy for denormalization (#422) Feb 22, 2020
typescript-tests fix: Add types for fallback strategy (#441) Apr 18, 2020
.babelrc.js Enable loose transformation for object spread operator to improve per… Jan 14, 2020
.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.6.0 Feb 22, 2020
CONTRIBUTING.md add contributing guidelines for issues Jan 2, 2017
LICENSE Add Dan back to LICENSE Dec 19, 2016
README.md Remove greenkeeper badge from readme Jan 23, 2020
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 fix: Add types for fallback strategy (#441) Apr 18, 2020
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.6.0 Feb 22, 2020
rollup.config.js [chore] update rollup May 29, 2019
yarn.lock [chore] Bump acorn from 5.5.3 to 5.7.4 (#436) Mar 14, 2020

README.md

normalizr build status Coverage Status npm version npm downloads

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.