Jolt List Transformation技术问题:移除键空格及列表映射方法
Jolt JSON转换:移除键名空格并映射列表数据
需求
- 不新增列表条目,保持原数据的列表结构
- 移除键名中的空格(如
Total BYOP Tier转为TotalBYOPTier) - 将嵌套数据正确映射到当前列表层级
输入数据
[ { "id": "9164339966213408050", "actualEndDate": "2022-08-15T14:47:09+03:00", "extendedChars": [ { "Total BYOP Tier": { "values": [ { "value": "60" } ], "name": "Total BYOP Tier", "isMultiple": false, "tomsId": "9162933742365468326" } } ], "chars": { "Total BYOP Tier": "60" } }, { "id": "9164339966213408051", "actualEndDate": "2022-08-15T14:47:09+03:00", "extendedChars": [ { "Total BYOP Tier": { "values": [ { "value": "60" } ], "name": "Total BYOP Tier", "isMultiple": false, "tomsId": "9162933742365468325" } } ], "chars": { "Total BYOP Tier": "60" } } ]
预期输出
[ { "id": "9164339966213408050", "actualEndDate": "2022-08-15T14:47:09+03:00", "extendedChars": [ { "values": [ { "value": "60" } ], "name": "Total BYOP Tier", "isMultiple": false, "tomsId": "9162933742365468326", "TotalBYOPTier": { "values": [ { "value": "60" } ], "name": "Total BYOP Tier", "isMultiple": false, "tomsId": "9162933742365468326" } } ], "chars": { "TotalBYOPTier": "60" } }, { "id": "9164339966213408051", "actualEndDate": "2022-08-15T14:47:09+03:00", "extendedChars": [ { "values": [ { "value": "60" } ], "name": "Total BYOP Tier", "isMultiple": false, "tomsId": "9162933742365468325", "TotalBYOPTier": { "values": [ { "value": "60" } ], "name": "Total BYOP Tier", "isMultiple": false, "tomsId": "9162933742365468325" } } ], "chars": { "TotalBYOPTier": "60" } } ]
尝试过的Spec
[ { "operation": "shift", "spec": { "*": { "@": "CustomerProductArray" } } }, { "operation": "shift", "spec": { "CustomerProductArray": { "*": { "extendedChars": { "*": { "* * *": "CustomerProductArray.&(0,1)&(0,2)&(0,3)", "*": { "name": "CustomerProductArray[].extendedChars[]..name", "isMultiple": "CustomerProductArray[].extendedChars[].isMultiple", "tomsId": "CustomerProductArray[].extendedChars[].tomsId", "values": "CustomerProductArray[].extendedChars[].values" } } }, "chars": { "* * *": "CustomerProductArray[].chars.&(0,1)&(0,2)&(0,3)" } } } } } ]
正确的Jolt转换Spec
[ { "operation": "shift", "spec": { "*": { "id": "[&1].id", "actualEndDate": "[&1].actualEndDate", "extendedChars": { "*": { "* * *": { "@": "[&4].extendedChars[&2].&(0,1)&(0,2)&(0,3)", "values": "[&4].extendedChars[&2].values", "name": "[&4].extendedChars[&2].name", "isMultiple": "[&4].extendedChars[&2].isMultiple", "tomsId": "[&4].extendedChars[&2].tomsId" } } }, "chars": { "* * *": "[&2].chars.&(0,1)&(0,2)&(0,3)" } } } } ]
转换逻辑说明
- 层级定位:通过
&1、&2、&4等索引精准定位原数据的层级,确保映射到原列表的对应条目,不会打乱顺序或新增条目。 - 键名处理:利用
&(0,1)&(0,2)&(0,3)拆分带空格的键名(Total/BYOP/Tier)并拼接成无空格的新键TotalBYOPTier。 - 字段平级映射:将
extendedChars内部嵌套对象的核心字段(values/name等)直接映射到当前层级,同时保留新生成的无空格键的完整结构。 - chars对象转换:直接将原带空格的键转换为无空格键,保留对应的值。
内容的提问来源于stack exchange,提问作者NoOoNY
相关产品推荐
相关产品推荐

