I need to apply a JOLT transform to some JSON.
Example input JSON is...
{
"allergen" : [ "Peanuts", "Egg", "Milk" ],
"diagnosed_by_doc" : "Yes",
"diagnosis_age" : "3-5 years old",
"doc_type" : [ "Allergist", "Dermatologist", "Other Healthcare Provider" ],
"tests" : [ "Skin prick test", "Blood tests", "Oral food challenge" ],
"outgrown" : "No"
}
The expected output would be:
{
"allergen" : "Peanuts",
"diagnosed_by_doc" : "Yes",
"diagnosis_age" : "3-5 years old",
"diagnosed_by_allergist": true,
"diagnosed_by_dermatologist": true,
"diagnosed_by_other_healthcare_provider": true,
"skin_prick_test": true,
"blood_test": true,
"oral_food_challenge": true,
"outgrown" : "No"
},
{
"allergen" : "Egg",
"diagnosed_by_doc" : "Yes",
"diagnosis_age" : "3-5 years old",
"diagnosed_by_allergist": true,
"diagnosed_by_dermatologist": true,
"diagnosed_by_other_healthcare_provider": true,
"skin_prick_test": true,
"blood_test": true,
"oral_food_challenge": true,
"outgrown" : "No"
},
{
"allergen" : "Milk",
"diagnosed_by_doc" : "Yes",
"diagnosis_age" : "3-5 years old",
"diagnosed_by_allergist": true,
"diagnosed_by_dermatologist": true,
"diagnosed_by_other_healthcare_provider": true,
"skin_prick_test": true,
"blood_test": true,
"oral_food_challenge": true,
"outgrown" : "No"
},
How can I apply two transforms to this JSON?
- Each item in the
allergen
list becomes its own JSON object. - Each item in the
doc_type
andtests
list get "converted to boolean". That is to say, if thedoc_type
list containsAllergist
then the resulting JSON object would containdiagnosed_by_allergist: true
. If the phrase "Allergist" is not present then it's not necessary for the resulting object to containdiagnosed_by_allergist: false
. (This is more of a conditional operation than a data conversion).
In the original JSON, all the subsequent data applies to each allergen. So, for example, diagnosis_age
applies the same to the resulting Peanuts
, Egg
, and Milk
JSON objects.
What JOLT transform spec can I use to "flatten" the allergen
list into individual objects and conditionally present fields?
CodePudding user response:
You can walk through the allergen
list by using "@"
wildcard individually to represent each attributes while using conditional logic through use of object representation by curly braces for the lists doc_type
and tests
within the first shift transformation spec, and the second spec is used to remove object key names such as
[
{
"operation": "shift",
"spec": {
"allergen": {
"*": {
"@": "&1.&2",
"@(2,diagnosed_by_doc)": "&.diagnosed_by_doc",
"@(2,diagnosis_age)": "&.diagnosis_age",
"@(2,doc_type)": {
"*": {
"Allergist": {
"#true": "&3.diagnosed_by_allergist"
},
"Dermatologist": {
"#true": "&3.diagnosed_by_dermatologist"
},
"Other Healthcare Provider": {
"#true": "&3.diagnosed_by_other_healthcare_provider"
}
}
},
"@(2,tests)": {
"*": {
"Skin prick test": {
"#true": "&3.diagnosed_by_skin_prick_test"
},
"Blood tests": {
"#true": "&3.diagnosed_by_blood_test"
},
"Oral food challenge": {
"#true": "&3.diagnosed_by_oral_food_challenge"
}
}
},
"@(2,outgrown)": "&.outgrown"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": ""
}
}
]