使用Jolt转换JSON:将字符串列表拆分转为指定键值对
使用Jolt转换JSON数据:字符串列表转键值对
修正后的输入JSON(原输入存在重复键问题,已合并为有效数组)
{ "id": "abc", "type": "car", "size": "small", "carInformation": [ { "Licence": { "code": "AP123" }, "additionalInformation": [ "Brought successfully", "/identification/", "/Rate/30000", "/Date/2023-01-11" ] }, { "Licence": { "code": "AP124" }, "additionalInformation": [ "Brought unsuccessfully", "/identification/98981", "/Rate/30030", "/Date/2023-01-12" ] } ] }
需求说明
- 遍历
carInformation数组及其中的additionalInformation数组 - 按
/拆分字符串:- 拆分后提取键值(如
/Rate/30000→ 键Rate,值30000),同键值收集为数组 - 不含
/的字符串,按出现顺序以DATA{序号}为键存储内容
- 拆分后提取键值(如
Jolt转换规范
[ { "operation": "shift", "spec": { "carInformation": { "*": { "additionalInformation": { "*": { "/": { "@1": { "/.*?/.*": { "$(0,1)": "[&3]", "$(0,2)": "@" }, "/.*?/": { "$(0,1)": "[&3]", "#null": "@" } } }, "!/": { "$": "DATA[]" } } } } } } }, { "operation": "shift", "spec": { "*": { "*": "&1.&", "DATA": { "*": "&2.DATA$(1,1)" } } } }, { "operation": "shift", "spec": { "*": "cars.carInformation.&" } }, { "operation": "modify-overwrite-beta", "spec": { "cars": { "carInformation": { "identification": { "*": ["=notNull", null] } } } } } ]
转换说明
- 第一步Shift:展开数组,拆分含
/的字符串提取键值,将无/的字符串收集到DATA数组 - 第二步Shift:将
DATA数组转为DATA1、DATA2格式的键值对 - 第三步Shift:把所有结果包装到
cars.carInformation结构中 - 第四步Modify:处理空值(如第一个
identification的空内容,设置为null;若不需要可删除此步骤)
输出结果
{ "cars": { "carInformation": { "identification": [null, "98981"], "Rate": ["30000", "30030"], "Date": ["2023-01-11", "2023-01-12"], "DATA1": "Brought successfully", "DATA2": "Brought unsuccessfully" } } }
内容的提问来源于stack exchange,提问作者user20533754
相关产品推荐
相关产品推荐

