You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -13,30 +13,34 @@ A Node package for transforming(mapping) one JSON object to another based on a s
13
13
14
14
#### Input
15
15
var input = {
16
-
"id": 101,
17
-
"content": "My first npm package",
18
-
"latitude": 28.7123,
19
-
"longitude": 78.72346,
20
-
"user": {
21
-
"id": 201,
22
-
"name": "Codeslayer",
16
+
"id": 101,
17
+
"content": "My first npm package",
18
+
"latitude": 28.7123,
19
+
"longitude": 78.72346,
20
+
"user": {
21
+
"id": 201,
22
+
"name": "Codeslayer",
23
23
"interests": [
24
24
{
25
25
"id": 301,
26
26
"tag_id": 401,
27
27
"tag": "Food",
28
28
"types": {
29
-
"id": 501,
29
+
"id": 501,
30
30
"name": "Fast foods",
31
31
"description": "Unhealthy",
32
+
"extras": {
33
+
"price": "high",
34
+
"health": "low"
35
+
},
32
36
"contents": [
33
37
{
34
-
"id": 601,
35
-
"name": "Pizza"
38
+
"id": 601,
39
+
"name": "Pizza"
36
40
},
37
41
{
38
-
"id": 602,
39
-
"name": "Chicken Rice"
42
+
"id": 602,
43
+
"name": "Chicken Rice"
40
44
}
41
45
]
42
46
},
@@ -48,17 +52,25 @@ A Node package for transforming(mapping) one JSON object to another based on a s
48
52
"tag_id": 402,
49
53
"tag": "Movie",
50
54
"types": {
51
-
"id": 502,
55
+
"id": 502,
52
56
"name": "Comedy",
53
57
"description": "Fun to watch",
58
+
"extras": {
59
+
"price": "medium",
60
+
"entertainment": "decent"
61
+
},
54
62
"contents": [
55
63
{
56
-
"id" : 603,
57
-
"name" : "Scary Movie"
64
+
"id": 603,
65
+
"name": "Scary Movie"
66
+
},
67
+
{
68
+
"id": 604,
69
+
"name": "Jumanji"
58
70
},
59
71
{
60
-
"id" : 604,
61
-
"name": "Jumanji"
72
+
"id": 605,
73
+
"name": "Gravity"
62
74
}
63
75
]
64
76
},
@@ -74,39 +86,51 @@ A template specifies how the input json should be transformed to the desired out
74
86
75
87
**Update**: Starting version **0.1.4**, all keys to be included(and not to be renamed) in output json can be defined in an array named `includeTheseKeys` instead of mapping them one to one(see sample). Prior to **0.1.4**, all keys to be included in output needs to be mapped one to one.
76
88
89
+
**Update 2**: Version **0.1.5** and above, now support mapping nested keys using **.** as identifier for nesting. Ref example.
90
+
77
91
**Example:** In the sample below, `id` key from input transforms to `user_id` key in output json. `content` and `latitude` key need not be renamed so they are defined in `includeTheseKeys` array.
78
92
79
93
For the nested `user` object, the desired key is `userDetails`, so in the output, the data(specified by template `desiredData`) for `user` object comes under the key `userDetails`. The `longitude` key is not specified in template, so it is omitted from the ouput json.
80
94
81
95
var template = {
82
-
"id" : "user_id",
83
-
"includeTheseKeys" : ["content", "latitude],
84
-
"user" : {
96
+
"id": "user_id",
97
+
"includeTheseKeys": [
98
+
"content",
99
+
"latitude"
100
+
],
101
+
"user": {
85
102
"desiredKey": "userDetails",
86
103
"desiredData": {
87
104
"id": "id",
88
105
"name": "first_name",
89
106
"interests": {
90
107
"desiredKey": "interests",
91
-
"desiredData": [{
108
+
"desiredData": [
109
+
{
92
110
"id": "id",
93
111
"tag": "tag",
112
+
"types.name": "types_name",
113
+
"types.extras.health": "extras_health",
114
+
"types.contents[2].name": "third_content",
94
115
"types": {
95
116
"desiredKey": "mappedTypes",
96
117
"desiredData": {
97
-
"id": "type_id",
118
+
"id": "type_id",
98
119
"name": "type_name",
99
120
"description": "type_description",
100
121
"contents": {
101
122
"desiredKey": "mappedContents",
102
-
"desiredData": [{
103
-
"id" : "id",
104
-
"name" : "name"
105
-
}]
123
+
"desiredData": [
124
+
{
125
+
"id": "id",
126
+
"name": "name"
127
+
}
128
+
]
106
129
}
107
130
}
108
131
}
109
-
}]
132
+
}
133
+
]
110
134
}
111
135
}
112
136
}
@@ -117,59 +141,73 @@ For the nested `user` object, the desired key is `userDetails`, so in the output
117
141
var output = JSONTransform.transform(input, template);
0 commit comments