Skip to content

Commit c6c9f51

Browse files
Akash PorwalAkash Porwal
authored andcommitted
[FEATURE]
- Added Utils class - Added function to transform object from one format to another based on template
1 parent d902e3d commit c6c9f51

File tree

5 files changed

+321
-2
lines changed

5 files changed

+321
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ node_modules
3131

3232
# Optional REPL history
3333
.node_repl_history
34+
35+
.idea/

README.md

Lines changed: 160 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,160 @@
1-
# JSON-Mapper
2-
Node package for mapping(transforming) one JSON object into another based on a specified template
1+
JSON-Mapper
2+
=========
3+
4+
A Node package for mapping(transforming) one JSON object into another based on a specified template
5+
6+
## Installation
7+
8+
npm install json-mapper --save
9+
10+
## Usage
11+
12+
var JSONMapper = require('json-mapper');
13+
var input = {
14+
"id" : 101,
15+
"content" : "My first npm package",
16+
"latitude" : 28.7123,
17+
"longitude" : 78.72346,
18+
"user" : {
19+
"id" : 201,
20+
"name" : "Codeslayer",
21+
"interests": [
22+
{
23+
"id": 301,
24+
"tag_id": 401,
25+
"tag": "Food",
26+
"types": {
27+
"id" : 501,
28+
"name": "Fast foods",
29+
"description": "Unhealthy",
30+
"contents": [
31+
{
32+
"id" : 601,
33+
"name" : "Pizza"
34+
},
35+
{
36+
"id" : 602,
37+
"name" : "Chicken Rice"
38+
}
39+
]
40+
},
41+
"createdAt": "2016-03-12T14:59:33.000Z",
42+
"updatedAt": "2016-03-12T14:59:33.000Z"
43+
},
44+
{
45+
"id": 302,
46+
"tag_id": 402,
47+
"tag": "Movie",
48+
"types": {
49+
"id" : 502,
50+
"name": "Comedy",
51+
"description": "Fun to watch",
52+
"contents": [
53+
{
54+
"id" : 603,
55+
"name" : "Scary Movie"
56+
},
57+
{
58+
"id" : 604,
59+
"name" : "Jumanji"
60+
}
61+
]
62+
},
63+
"createdAt": "2016-03-12T14:59:33.000Z",
64+
"updatedAt": "2016-03-12T14:59:33.000Z"
65+
}
66+
]
67+
}
68+
};
69+
70+
var template = {
71+
"id" : "user_id",
72+
"content" : "content",
73+
"latitude" : "latitude",
74+
"user" : {
75+
"desiredKey": "userDetails",
76+
"desiredData": {
77+
"id": "id",
78+
"name": "first_name",
79+
"interests": {
80+
"desiredKey": "interests",
81+
"desiredData": [{
82+
"id": "id",
83+
"tag": "tag",
84+
"types": {
85+
"desiredKey": "mappedTypes",
86+
"desiredData": {
87+
"id" : "type_id",
88+
"name": "type_name",
89+
"description": "type_description",
90+
"contents": {
91+
"desiredKey": "mappedContents",
92+
"desiredData": [{
93+
"id" : "id",
94+
"name" : "name"
95+
}]
96+
}
97+
}
98+
}
99+
}]
100+
}
101+
}
102+
}
103+
};
104+
105+
var result = JSONMapper.map(input, template);
106+
107+
##### Result
108+
{
109+
"user_id": 101,
110+
"content": "My first npm package",
111+
"latitude": 28.7123,
112+
"userDetails": {
113+
"id": 201,
114+
"first_name": "Codeslayer",
115+
"interests": [
116+
{
117+
"id": 301,
118+
"tag": "Food",
119+
"mappedTypes": {
120+
"type_id": 501,
121+
"type_name": "Fast foods",
122+
"type_description": "Unhealthy",
123+
"mappedContents": [
124+
{
125+
"id": 601,
126+
"name": "Pizza"
127+
},
128+
{
129+
"id": 602,
130+
"name": "Chicken Rice"
131+
}
132+
]
133+
}
134+
},
135+
{
136+
"id": 302,
137+
"tag": "Movie",
138+
"mappedTypes": {
139+
"type_id": 502,
140+
"type_name": "Comedy",
141+
"type_description": "Fun to watch",
142+
"mappedContents": [
143+
{
144+
"id": 603,
145+
"name": "Scary Movie"
146+
},
147+
{
148+
"id": 604,
149+
"name": "Jumanji"
150+
}
151+
]
152+
}
153+
}
154+
]
155+
}
156+
}
157+
158+
## Release History
159+
160+
* 0.1.0 Initial release

Utils.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Created by codeslayer on 3/13/16.
3+
*/
4+
5+
module.exports = {
6+
7+
/**
8+
* This function checks for empty/null variable/object
9+
*
10+
* @param obj
11+
* @return boolean
12+
*/
13+
isEmpty:function (obj) {
14+
// Speed up calls to hasOwnProperty
15+
var hasOwnProperty = Object.prototype.hasOwnProperty;
16+
// null and undefined are "empty"
17+
if (obj == null) return true;
18+
19+
// Assume if it has a length property with a non-zero value
20+
// that that property is correct.
21+
if (obj.length && obj.length > 0) return false;
22+
if (obj.length === 0) return true;
23+
24+
if(obj.toString())
25+
// Otherwise, does it have any properties of its own?
26+
// Note that this doesn't handle
27+
// toString and valueOf enumeration bugs in IE < 9
28+
for (var key in obj) {
29+
if (hasOwnProperty.call(obj, key)) return false;
30+
}
31+
if (!isNaN(obj)) {
32+
if (obj.toString().length > 0) return false;
33+
}
34+
35+
return true;
36+
},
37+
38+
/**
39+
* Function to check if the variable is an array
40+
*
41+
* @param a
42+
* @return boolean
43+
*/
44+
isArray: function(a) {
45+
return (!!a) && (a.constructor === Array);
46+
},
47+
48+
/**
49+
* Function to check if the variable is an object
50+
*
51+
* @param a
52+
* @return boolean
53+
*/
54+
isObject: function(a) {
55+
return (!!a) && (a.constructor === Object);
56+
}
57+
58+
};

index.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Created by codeslayer on 3/13/16.
3+
*/
4+
5+
var JSONMapper = {
6+
7+
/**
8+
* Routes request to mapObject function after sanitation checks in input/mapping
9+
*
10+
* @param {JSON} input, {JSON} mapping
11+
* @return {JSON}
12+
*/
13+
map: function(input, mapping){
14+
if(Utils.isEmpty(input) || Utils.isEmpty(mapping)){
15+
return null
16+
}
17+
18+
return JSONMapper.mapObject(input,mapping);
19+
},
20+
21+
/**
22+
* This function iterates the specified mapping and populates the desired keys from the input
23+
*
24+
* @param {JSON} input, {JSON} mapping
25+
* @return {JSON}
26+
*/
27+
mapObject: function(input, mapping){
28+
var mappedObject = {};
29+
30+
for(var actualKey in mapping){
31+
/*
32+
-> if an object or array needs to be mapped, we take the desired key name and data to be mapped inside an object,
33+
and parse all the keys in data one by one and set the mapped output against the desired key name
34+
-> if a single variable needs to be mapped, we set the desired value against the desired key
35+
*/
36+
if(Utils.isObject(mapping[actualKey])){
37+
var desiredKeyName = mapping[actualKey]['desiredKey'];
38+
var desiredDataMapping = mapping[actualKey]['desiredData'];
39+
40+
if(Utils.isObject(desiredDataMapping)){
41+
mappedObject[desiredKeyName] = JSONMapper.mapObject(input[actualKey],desiredDataMapping);
42+
}
43+
else if(Utils.isArray(desiredDataMapping)){
44+
mappedObject[desiredKeyName] = JSONMapper.mapArray(input[actualKey],desiredDataMapping[0]);
45+
}
46+
}
47+
else{
48+
var desiredKeyName = mapping[actualKey];
49+
mappedObject[desiredKeyName] = input[actualKey];
50+
}
51+
}
52+
53+
return mappedObject;
54+
},
55+
56+
/**
57+
* This function iterates an array and maps each object of the array one by one
58+
*
59+
* @param {JSON} input, {JSON} mapping
60+
* @return {JSON Array}
61+
*/
62+
mapArray: function(input, mapping){
63+
var mappedArray = [];
64+
for(var index in input){
65+
var inputObject = input[index];
66+
var mappedObject = JSONMapper.mapObject(inputObject,mapping);
67+
mappedArray.push(mappedObject);
68+
}
69+
70+
return mappedArray;
71+
}
72+
73+
};
74+
75+
module.exports = JSONMapper;

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "json-mapper",
3+
"version": "0.1.0",
4+
"description": "Node package for mapping(transforming) one JSON object into another based on a specified template",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/codeslayer1/JSON-Mapper.git"
12+
},
13+
"keywords": [
14+
"json",
15+
"mapping",
16+
"mapper",
17+
"transform",
18+
"convert"
19+
],
20+
"author": "codeslayer1",
21+
"license": "ISC",
22+
"bugs": {
23+
"url": "https://github.com/codeslayer1/JSON-Mapper/issues"
24+
},
25+
"homepage": "https://github.com/codeslayer1/JSON-Mapper#readme"
26+
}

0 commit comments

Comments
 (0)