Created
January 7, 2015 18:43
-
-
Save DinoChiesa/49999b14374f9b367fef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// xformResponse.js | |
// ------------------------------------------------------------------ | |
// | |
// pre-transform the response before the XML-to-JSON step. | |
// | |
// created: Wed Jan 7 08:06:03 2015 | |
// last saved: <2015-January-07 10:41:38> | |
// expected input: | |
// | |
// { | |
// "school":{ | |
// "teachers" : [ | |
// {"students" :[{"name": "student1"}, {"name": "student2"}], "name" : "teacher1"}, | |
// {"students" :[{"name": "student3"}, {"name": "student4"}], "name" : "teacher2"} | |
// ] | |
// } | |
// } | |
// | |
// desired output: | |
// | |
// { | |
// school:{ | |
// teachers : { | |
// teacher : [ | |
// {students : { name: [student1,student2} }, name : teacher1}, | |
// {students : { name: [student3,student4} }, name : teacher2} | |
// ] | |
// } | |
// } | |
// } | |
// | |
// Running the output through the JSON-to-XML step will produce the | |
// desired XML output, which looks like: | |
// | |
// <school> | |
// <teacher> | |
// <name>teacher1</name> | |
// <students> | |
// <name>student1</name> | |
// <name>student2</name> | |
// </students> | |
// </teacher> | |
// <teacher> | |
// <name>teacher1</name> | |
// <students> | |
// <name>student1</name> | |
// <name>student2</name> | |
// </students> | |
// </teacher> | |
// </school> | |
function parseContent(content) { | |
var ctype = context.getVariable('response.header.content-type') + ''; | |
if (ctype.indexOf('application/json') === 0) { // starts with | |
content = JSON.parse(content); | |
} | |
return content; | |
} | |
function fixupArray(obj, propName, accessor) { | |
var type = Object.prototype.toString.call(obj), i, | |
a = [], rval = {}; | |
if (null !== obj) { | |
if (type === "[object Array]") { | |
for (i=0; i<obj.length; i++) { | |
a.push(accessor(obj[i])); // string | |
} | |
rval[propName] = a; | |
} | |
} | |
return rval; | |
} | |
function fixupStudents(obj) { | |
return fixupArray(obj, 'name', function(o){return o.name;}); | |
} | |
function fixupTeachers(obj) { | |
return fixupArray(obj, 'teacher', function(o){return o;}); | |
} | |
// ******************************************** | |
// Execution starts here | |
var c = context.getVariable('response.content') + '', | |
body = parseContent(c); | |
if (body.school) { | |
if (body.school.teachers) { | |
body.school.teachers.forEach(function(item){ | |
if (item.students) { | |
item.students = fixupStudents(item.students); | |
} | |
}); | |
// un-comment the following line to get a parent item | |
// around all the teacher items. | |
//body.school.teachers = fixupTeachers(body.school.teachers); | |
} | |
} | |
context.setVariable('response.content', JSON.stringify(body, null, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment