如何通过Jolt Spec将SpecList字符串列表转为对象列表?
Jolt转换需求:将SpecList字符串列表转为对象列表
输入JSON
{ "Address": "ABC", "latitude": "xy.zx", "longitude": "xy.zm", "name": "name", "pincode": "123456", "registrationId": "xxcd", "Doctor": [ { "Name": "ABC", "SpecList": [ "A", "B", "C" ], "practitonerId": "z", "location": { "lat": "ab.cd", "lng": "ab.cd" } }, { "Name": "def", "SpecList": [ "D", "E" ], "practitonerId": "22", "location": { "lat": "ef.gh", "lng": "ef.gh" } } ] }
当前无效的Jolt规则
[ { "operation": "shift", "spec": { "Address": "hospitalAddress", "latitude": "hospitalLatitude", "longitude": "hospitalLongitude", "name": "hospitalName", "pincode": "hospitalPincode", "registrationId": "hospitalRegistrationId", "Doctor": { "*": { "Name": "doctors[#2].doctorName", "practitonerId": "doctors[#2].doctorPractitionerId", "SpecList": { "*": { "@": "doctors[#1].doctorSpecializations.[].specializationName" } } } } } } ]
期望输出
{ "hospitalAddress" : "ABC", "hospitalLatitude" : "xy.zx", "hospitalLongitude" : "xy.zm", "hospitalName" : "name", "hospitalPincode" : "123456", "hospitalRegistrationId" : "xxcd", "doctors" : [ { "doctorName" : "abc", "doctorPractitionerId" : "z", "doctorSpecializations" : [ { "specializationName" : "A" }, { "specializationName" : "B" }, { "specializationName": "C" } ] }, { "doctorName" : "def", "doctorPractitionerId" : "22", "doctorSpecializations" : [ { "specializationName" : "D" }, { "specializationName" : "E" } ] }] }
修正后的Jolt规则
原规则的问题在于SpecList转换时的索引引用错误:Doctor下的*对应医生数组的下标(层级索引为#2),SpecList下的*对应专长数组的下标(层级索引为#1),因此需要用#3来关联到对应的医生条目。
修正后的规则如下:
[ { "operation": "shift", "spec": { "Address": "hospitalAddress", "latitude": "hospitalLatitude", "longitude": "hospitalLongitude", "name": "hospitalName", "pincode": "hospitalPincode", "registrationId": "hospitalRegistrationId", "Doctor": { "*": { "Name": "doctors[#2].doctorName", "practitonerId": "doctors[#2].doctorPractitionerId", "SpecList": { "*": { "@": "doctors[#3].doctorSpecializations[].specializationName" } } } } } }, // 可选:若需将doctorName转为小写,添加此操作 { "operation": "modify-overwrite-beta", "spec": { "doctors": { "*": { "doctorName": "=toLowerCase" } } } } ]
说明
- 索引修正:将
doctors[#1]改为doctors[#3],确保每个专长项正确归属到对应的医生条目。 - 小写转换:如果需要和期望输出一致,将
doctorName转为小写,可添加modify-overwrite-beta操作执行toLowerCase转换。
内容的提问来源于stack exchange,提问作者Abhilash Gupta
相关产品推荐
相关产品推荐

