npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-api-normalize

v1.1.5

Published

Normalize JSON api dataset

Downloads

2,881

Readme

JSON api normalize

Build Status Coverage Status NPM Status

A simple way to traverse datasets based on JSON API specification. Normalize is a lightweight javascript library with simple and powerful api. Has no dependencies and weighs less than 1KB.

Visit documentation site.

"If you’ve ever argued with your team about the way your JSON responses should be formatted, JSON API can be your anti-bikeshedding tool." If you are new to JSON api we recommend you browse json api website and examples to familiarize yourself with specification. This library is built upon standards and conventions of JSON api and provides a simple way to traverse and retrieve all those attributes and relations.

Api and examples

Lets start with a typical JSON api formatted dataset:

articleJsonApiData = {
    data: {
        type: 'article',
        id: '1',
        attributes: {
            title: 'JSON API paints my bikeshed!',
            body: 'The shortest article. Ever.'
        },
        relationships: {
            author: {data: {id: '42', type: 'user'}},
            publisher: {data: {id: '24', type: 'user'}},
            tags: {data: [
                {id: '1', 'type': 'tag'},
                {id: '2', 'type': 'tag'}
            ]}
        }
    },
    included: [{
        type: 'user',
        id: '42',
        attributes: {
            firstName: 'John',
            lastName: 'Doe',
        },
        relationships: {
            boss: {'data': {'id': '42', 'type': 'user'}},
        }
    }, {
        type: 'tag',
        id: '1',
        attributes: {
            name: 'tag 1'
        }
    }, {
        type: 'tag',
        id: '2',
        attributes: {
            name: 'tag 2'
        }
    }]
};

Using JSON api normalize we can retrive simple data like so:

normalize(articleJsonApiData).get('title');
// will return 'JSON API paints my bikeshed!

normalize(articleJsonApiData).get('author.firstName');
// will output 'John'

normalize(articleJsonApiData).get(['id', 'title', 'body']);
// will return
// {
//     id: '1',
//     title: 'JSON API paints my bikeshed!',
//     body: 'The shortest article. Ever.'
// }

Next example shows how to retrieve complex dataset that can include (circular) relation data:

normalize(articleJsonApiData).get([
    'id',
    'title',
    'body',
    'author.id',
    'author.firstName',
    'author.lastName',
    'author.boss.firstName',
    'author.boss.lastName',
    'tags.id',
    'tags.name',
]);

// will return
// {
//     id: '1',
//     title: 'JSON API paints my bikeshed!',
//     body: 'The shortest article. Ever.',
//     author: {
//         id: '42',
//         firstName: 'John',
//         lastName: 'Doe',
//         boss: {
//             firstName: 'John',
//             lastName: 'Doe'
//         }
//     },
//     tags: [{
//         id: '1',
//         name: 'tag 1'
//     }, {
//         id: '2',
//         name: 'tag 2'
//     }]
// };

Installation

Json api normalize is packaged as UMD library so you can use it both on client and server (CommonJS and AMD environment) or with browser globals.

// install via npm
npm install json-api-normalize --save

// if you use bundler
var normalize = require('json-api-normalize');

// or just using browser globals
var normalize = window.jsonApiNormalize;