normalizr 0,15,0,6,17,0,20,12 travis-ci npm webpack

Normalizes nested JSON according to a schema

2 years after MIT

normalizr build status Coverage Status npm version npm downloads

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.

Related Repositories

flux-react-router-example

flux-react-router-example

A sample app showcasing Flux with React Router ...

redux-entity

redux-entity

WIP. An abstraction layer around handling normalized entity storage and data fet ...

denormalizr

denormalizr

Denormalize data normalized with normalizr ...

normalizr-graphql

normalizr-graphql

creates a normalizr schema for a graphql query from a graphql schema ...

redux-entities

redux-entities

Higher-order reducer for store entities received from normalizr and makes it eas ...


Top Contributors

gaearon paularmstrong unindented babsonmatt vlkosinov fredrikolovsson YoruNoHikage chabou eyston evgenyrodionov lukekarrys alexaivars xuoe yoo2001818 fhelwanger frankwallis gpfunk jkusachi Kestutis bitantics martintietz puradox smashercosmo wbinnssmith tindli

Dependencies

package version
dev babel-cli ^6.18.0
babel-eslint ^7.1.1
babel-preset-es2015 ^6.18.0
babel-preset-es2015-rollup ^3.0.0
babel-preset-stage-1 ^6.16.0
coveralls ^2.11.15
eslint ^3.12.2
flow-bin ^0.37.1
immutable ^3.8.1
jest 19.0.2
mkdirp ^0.5.1
npm-run-all ^3.1.2
rimraf ^2.5.4
rollup ^0.37.0
rollup-plugin-babel ^2.7.1
rollup-plugin-filesize ^1.2.1
rollup-plugin-uglify ^1.0.1
typescript ^2.1.4
typescript-definition-tester 0.0.5

Releases

-   v2.2.1 zip tar
-   v2.2.0 zip tar
-   v2.1.0 zip tar
-   v2.0.2 zip tar
-   v2.0.1 zip tar
-   v2.0.0 zip tar
-   v1.4.1 zip tar
-   v1.4.0 zip tar
-   v1.3.1 zip tar
-   v1.3.0 zip tar
-   v1.2.0 zip tar
-   v1.1.0 zip tar
-   v1.0.0 zip tar
-   v0.1.3 zip tar
-   v0.1.2 zip tar
-   v0.1.1 zip tar