-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson-transform.js
More file actions
63 lines (61 loc) · 1.81 KB
/
json-transform.js
File metadata and controls
63 lines (61 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Calling external system example.
// This demonstrates use of the DataPower GatewayScript (ECMA) to transorm JSON to another JSON format.
// Author: Steve Edwards, Escala Ltd.
// Date : 2014-09-30.
// Note : this code is for demonstration purposes only, not production - level.
// Read the action input context as a JSON object
session.input.readAsJSON (function (error, input_json) {
if (error) {
// an error occurred when parsing the content, e.g. invalid JSON object
// uncatched error will stop the processing and the error will be logged
throw error;
}
var result =
{"order":
{"order-date":
{"day": input_json.o.od.y,
"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][(input_json.o.od.m - 1)],
"year" : input_json.o.od.y + 2000
}
},
"customer": {"name": input_json.o.c.cn, "address": input_json.o.c.ad, "phone": input_json.o.c.ph},
"order-number": input_json.on,
"date-time-received": new Date().toLocaleDateString() + ' - ' + new Date().toTimeString()
};
session.output.write(result);
});
/*
Sample input:
{
"o": {
"c": {
"cn": "Apex",
"ad": "999 Hollywood Ave, Los Angeles, CA",
"ph": "00-1-999-999-9999"
},
"od": {
"d": 31,
"m": 9,
"y": 15
}
},
"on": "ABC-123"
}
Sample output:
{
"order": {
"order-date": {
"day": 15,
"month": "Sep",
"year": 2015
}
},
"customer": {
"name": "Apex",
"address": "999 Hollywood Ave, Los Angeles, CA",
"phone": "00-1-999-999-9999"
},
"order-number": "ABC-123",
"date-time-received": "18 February 2015 - 11:32:05 GMT+0000 (GMT)"
}
*/