Payload 1: { "name": "Homer J. Simpson", "occupation": "Security inspector", "street": "Evergreen Terrace", "number": "742", "city": "Springfield", "state": "Kentucky", "parents": { "father": { "name": "Abraham", "age": "80" }, "mother": { "name": "Mona", "age": "70" } } } | Single template that works on payloads from inputs form left column and transformed JSON will be presented in right column.def deleteByKey(jsonObj, searchTerm) if (is-object($jsonObj)) {for ($jsonObj) .key: if (is-object(.value) or is-array(.value)) deleteByKey(.value, $searchTerm) else if (.key != $searchTerm) .value } else if (is-array($jsonObj)) [for($jsonObj) deleteByKey(., $searchTerm) ] { "PersonalInformation": deleteByKey(.PersonalInformation, "Ethnicity"), "ContactInformation": { "street": .ContactInformation.streetName, * - streetName :. }, "father": .parents.father.name, "mother": .parents.mother.name, "firstName": split(.FullName, " ")[0], "lastName": split(.FullName, " ")[1], * - parents, FullName : . } - The code above has one function which takes payload context and a search key as parameters and find the key from payload, remove it and return the rest of the payload. - The need of this complex functions is that we don’t know in which payload where the specific key will be and if we want to delete that we’ll need to do it manually which is not sufficient for different scenarios. Thus, we need this DFS based recursive function to achieve this task. INFO: JSLT is a turing complete language, that’s why we can achieve any kind of transformation of JSON in that. - The code below the function does some template based manual formatting like FLATTENING, INGORE, RENAME/REPLACE, DELETE etc. | Output of Payload 1:{ "father" : "Abraham", "mother" : "Mona", "name" : "Homer J. Simpson", "occupation" : "Security inspector", "street" : "Evergreen Terrace", "number" : "742", "city" : "Springfield", "state" : "Kentucky" } |
Payload 2:{ "FullName" : "Kari Nordmann Inger", "ContactInformation" : { "streetName" : "Karl johans gate", "City" : "Oslo", "PhoneNumber" : "88880000", "Ethnicity": "Hindu" }, "PersonalInformation" : { "Age" : "45", "Civil Status" : "Married", "Ethnicity" : "Norwegian" } } | Output of payload 2:{ "PersonalInformation" : { "Age" : "45", "Civil Status" : "Married" }, "ContactInformation" : { "street" : "Karl johans gate", "City" : "Oslo", "PhoneNumber" : "88880000", "Ethnicity" : "Hindu" }, "firstName" : "Kari", "lastName" : "Nordmann" } | |