Github repository for your stars.
$ npm install json-function
import { schema } from 'json-function';
Example Data
const data = [{id: 0,user: {firstname: "John",lastname: "Doe"},title: "Book Name"},{id: 1,user: {firstname: "Johnny",lastname: "Doe"},title: "Book Name 2"}];
Syntax
schema(data, {book: {id: "id",title: "title"},firstname: "user.firstname",lastname: "user.lastname"});
Output
[{firstname: "John",lastname: "Doe",book: {id: 0,title: "Book Name"}},{firstname: "Johnny",lastname: "Doe",book: {id: 1,title: "Book Name 2"}}];
Apart from this wonderful method, there are also some advanced uses.
Schema has a calback that hosts useful tools.
Example Data
const data = [{id: 0,user: {firstname: "John",lastname: "Doe"},title: "Book Name"},{id: 1,user: {firstname: "Johnny",lastname: "Doe"},title: "Book Name 2"}];
Syntax
join()
The default separator is a space.
schema(data, (sc) => ({id: 'id',fullName: sc.join('user.firstname', 'user.lastname')}));
Output
[{id: 0,fullName: "John Doe"},{id: 1,fullName: "Johnny Doe"}];
To use a custom separator.
schema(data, (sc) => ({id: 'id',fullName: sc.join('user.firstname', 'user.lastname', { separator: '_' })}));
Output
[{id: 0,fullName: "John_Doe"},{id: 1,fullName: "Johnny_Doe"}];
custom()
Example Data
const data = [{id: 0,createdAt: "2019-03-07T19:22:36+00:00",user: {firstname: "Aykut",lastname: "Kardaş"}},{id: 1,createdAt: "2019-03-02T19:22:36+00:00",user: {firstname: "John",lastname: "Doe"}}];
Usage
schema(data, sc => ({id: "id",fullName: sc.custom((firstname, lastname) => `${firstname.toUpperCase()} ${lastname.toUpperCase()}`,"user.firstname","user.lastname"),createdAt: sc.custom(createdAt => moment(createdAt).format("MM/DD/YYYY"),"createdAt"),idIncrement: sc.custom(id => id + 1, "id")}));
Output
[{id: 0,fullName: "AYKUT KARDAŞ",createdAt: "03/07/2019",idIncrement: 1},{id: 1,fullName: "JOHN DOE",createdAt: "03/02/2019",idIncrement: 2}]