https://github.com/maxatwork/jsonapi-normalizr
Schema-based JSON API data parser
https://github.com/maxatwork/jsonapi-normalizr
Last synced: about 1 year ago
JSON representation
Schema-based JSON API data parser
- Host: GitHub
- URL: https://github.com/maxatwork/jsonapi-normalizr
- Owner: maxatwork
- License: mit
- Created: 2017-02-02T18:49:55.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-05-07T20:06:31.000Z (about 8 years ago)
- Last Synced: 2025-06-02T15:17:21.007Z (about 1 year ago)
- Language: JavaScript
- Size: 16.6 KB
- Stars: 2
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Schema-based JSON API data parser
Quick and dirty implementation. Do not use in production!
## Usage
### Installation
```bash
npm i --save jsonapi-normalizr
```
### Imports
```javascript
import {schema, fields, normalize} from 'jsonapi-normalizr'
```
### Schemas
```javascript
const userSchema = schema('users', {
email: fields.string(),
fullName: fields.string({fromName: 'full_name'})
})
const commentSchema = schema('comments', {
author: fields.relationship(userSchema),
text: fields.string()
}, {jsonApiType: 'blog_comments'})
const postSchema = schema('posts', {
created: fields.date(),
title: fields.string(),
text: fields.string(),
comments: fields.relationship(commentSchema, {many: true})
}, {jsonApiType: 'blog_posts'})
```
### Source data
```javascript
const jsonApiData = {
data: {
id: '1',
type: 'blog_posts',
attributes: {
created: '2017-02-02T12:30:41Z',
title: 'Hello world!',
text: 'This is the post about jsonapi-normalizr'
},
relationships: {
comments: {
data: [
{type: 'blog_comments', id: '1'},
{type: 'blog_comments', id: '2'}
]
}
}
},
included: [
{
id: '1',
type: 'blog_comments',
attributes: {text: 'First comment'},
relationships: {
author: {data: {type: 'users', id: '1'}}
}
},
{
id: '2',
type: 'blog_comments',
attributes: {text: 'Second comment'},
relationships: {
author: {data: {type: 'users', id: '2'}}
}
},
{
id: '1',
type: 'users',
attributes: {email: 'user1@example.com', full_name: 'John Doe'}
},
{
id: '2',
type: 'users',
attributes: {email: 'user2@example.com', full_name: 'Dow Jones'}
}
]
}
```
### Parsing
```javascript
const normalized = normalize(
postSchema({include: ['comments', 'comments.author']}),
jsonApiData
)
```
### Result data
```javascript
{
"result": {"type": "posts", "id": "1"},
"entities": {
"posts": {
"1": {
"id": "1",
"created": new Date('2017-02-02T12:30:41Z'),
"title": "Hello world!",
"text": "This is the post about jsonapi-normalizr",
"comments": [
{"type": "comments", "id": "1"},
{"type": "comments", "id": "2"}
]
}
},
"comments": {
"1": {
"id": "1",
"author": {"type": "users", "id": "1"},
"text": "First comment"
},
"2": {
"id": "2",
"author": {"type": "users", "id": "2"},
"text": "Second comment"
}
},
"users": {
"1": {
"id": "1",
"email": "user1@example.com",
"fullName": "John Doe"
},
"2": {
"id": "2",
"email": "user2@example.com",
"fullName": "Dow Jones"
}
}
}
}
```