Skip to content

Commit ebdbb43

Browse files
Akash PorwalAkash Porwal
authored andcommitted
[FEATURE] - Added functionality to define all keys to be included in output json, in an array instead of mapping one to one
1 parent 60bb78d commit ebdbb43

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,17 @@ A Node package for transforming(mapping) one JSON object to another based on a s
7070
};
7171

7272
#### Template
73-
A template specifies how the input json should be transformed to the desired output json. The key value pair in template json specifies the input key to desired key name transformation(can be same if key need not be renamed). In case of nested json object/array, the desired key name is specified explicitly and the desired data specifies the template to be used for transforming inner object. The input keys not specified in the template are filtered out in the output json.
73+
A template specifies how the input json should be transformed to the desired output json. The key value pair in template json specifies the input key to desired key name transformation(can be same if key need not be renamed). In case of nested json object/array, the desired key name is specified explicitly and the desired data specifies the template to be used for transforming inner object. The input keys not specified in the template are filtered out in the output json.
7474

75-
**Example:** In the sample below, `id` key from input transforms to `user_id` key in output json. `content` key is not renamed since both input and output key name are same. 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.
75+
**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+
77+
**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+
79+
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.
7680

7781
var template = {
7882
"id" : "user_id",
79-
"content" : "content",
80-
"latitude" : "latitude",
83+
"includeTheseKeys" : ["content", "latitude],
8184
"user" : {
8285
"desiredKey": "userDetails",
8386
"desiredData": {
@@ -169,3 +172,4 @@ A template specifies how the input json should be transformed to the desired out
169172
* 0.1.1 Fixed blank values in array
170173
* 0.1.2 Bug fix for array exceeding bounds
171174
* 0.1.3 Handling for undefined keys in JSON
175+
* 0.1.4 Added functionality to define all keys to be included in output json, in an array instead of mapping one to one

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var JSONTransform = {
3030

3131
if(Utils.isEmpty(input) || Utils.isEmpty(template))
3232
return null;
33-
33+
3434
for(var actualKey in template){
3535
/*
3636
-> if an object or array needs to be transformed, we take the desired key name and data to be transformed inside an object,
@@ -48,6 +48,13 @@ var JSONTransform = {
4848
transformedObject[desiredKeyName] = JSONTransform.transformArray(input[actualKey],desiredDatatemplate[0]);
4949
}
5050
}
51+
else if(actualKey == 'includeTheseKeys' && Utils.isArray(template[actualKey])){
52+
console.log("Inside includeTheseKeys with array :: %j",template[actualKey]);
53+
for(var index = 0; index < template[actualKey].length; index++){
54+
var key = template[actualKey][index];
55+
transformedObject[key] = input[key];
56+
}
57+
}
5158
else{
5259
var desiredKeyName = template[actualKey];
5360
transformedObject[desiredKeyName] = input[actualKey];

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json_transform",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "A Node package for transforming(mapping) one JSON object to another based on a specified template",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)