我需要对一些JSON应用一个JOLT转换。
输入<例子strong>JSON是…例子strong>
{
"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"
}
期望输出是:
{
"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"
},
我如何应用两个转换到这个JSON?
allergen
列表中的每个项目都成为自己的JSON对象。doc_type
和tests
列表中的每个项目都被转换为布尔值。也就是说,如果doc_type
列表包含Allergist
,那么结果JSON对象将包含diagnosed_by_allergist: true
。如果"过敏医生"这句话;不存在,则结果对象不需要包含diagnosed_by_allergist: false
。(这更像是一个条件操作,而不是数据转换)。
在原始JSON中,所有后续数据适用于每个过敏原。因此,例如,diagnosis_age
将相同的应用于结果Peanuts
,Egg
和Milk
JSON对象。
我可以使用什么JOLT转换规范来"平坦化"?allergen
列表到单独的对象和有条件地呈现字段?
您可以浏览allergen
"@"
在使用条件逻辑时,通过对列表doc_type
使用花括号表示对象,分别使用通配符表示每个属性andtests
在第一个内移位第二个规范用于删除对象键名,如
[
{
"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": {
"*": ""
}
}
]