如何使用JOLT转换实现指定JSON到目标JSON格式的转换?
JOLT JSON结构转换方案
源JSON
{ "id": 6, "identifier": "fa7242c8-6574-1c49-ju1f-5gad0659ecmt", "name": "App List", "structureType": "iteration", "hasChildren": true, "children": [ { "id": 8, "identifier": "9990bffb-3edf-4d0b-8584-a542f792de91", "name": "AppNameOne", "structureType": "iteration", "hasChildren": true, "children": [ { "id": 783, "identifier": "03283159-9a79-4f21-8ead-55c071448d54", "name": "AppNameOne_03.14", "structureType": "iteration", "hasChildren": false, "attributes": { "startDate": "2018-12-01T00:00:00Z", "finishDate": "2019-02-08T00:00:00Z" }, "url": "http://tfs.orgname.org:8080/tfs/orgname/1s2ldda4-coda-4a0g-bg01-ueg59d97b086/_apis/wit/classificationNodes/Iterations/AppNameOne/AppNameOne_03.14" }, { "id": 1029, "identifier": "cc6b1e3b-a360-4a1e-afd2-629ca30e2cdf", "name": "AppNameOne_03.15", "structureType": "iteration", "hasChildren": false, "attributes": { "startDate": "2020-02-01T00:00:00Z", "finishDate": "2020-04-08T00:00:00Z" }, "url": "http://tfs.orgname.org:8080/tfs/orgname/1s2ldda4-coda-4a0g-bg01-ueg59d97b086/_apis/wit/classificationNodes/Iterations/AppNameOne/AppNameOne_03.15" } ], "url": "http://tfs.orgname.org:8080/tfs/orgname/1s2ldda4-coda-4a0g-bg01-ueg59d97b086/_apis/wit/classificationNodes/Iterations/AppNameOne" }, { "id": 15, "identifier": "ddde31b8-33bd-4eb0-b531-b7958fc218d6", "name": "MyAppTwo", "structureType": "iteration", "hasChildren": true, "children": [ { "id": 1485, "identifier": "5257534c-1949-4302-b1d1-ed1e36123645", "name": "MyAppTwo_03.53", "structureType": "iteration", "hasChildren": false, "attributes": { "startDate": "2023-04-01T00:00:00Z", "finishDate": "2023-08-01T00:00:00Z" }, "url": "http://tfs.orgname.org:8080/tfs/orgname/1s2ldda4-coda-4a0g-bg01-ueg59d97b086/_apis/wit/classificationNodes/Iterations/MyAppTwo/MyAppTwo_03.53" }, { "id": 1506, "identifier": "eac635b2-0221-471d-a3da-1d15d000186e", "name": "MyAppTwo_03.54", "structureType": "iteration", "hasChildren": false, "attributes": { "startDate": "2023-08-01T00:00:00Z", "finishDate": "2024-11-29T00:00:00Z" }, "url": "http://tfs.orgname.org:8080/tfs/orgname/1s2ldda4-coda-4a0g-bg01-ueg59d97b086/_apis/wit/classificationNodes/Iterations/MyAppTwo/MyAppTwo_03.54" }, { "id": 1511, "identifier": "b1e1a5cc-f6cd-490e-9419-c3cf5e241fa9", "name": "MyAppTwo_03.52HF", "structureType": "iteration", "hasChildren": false, "attributes": { "startDate": "2023-07-17T00:00:00Z", "finishDate": "2024-11-14T00:00:00Z" }, "url": "http://tfs.orgname.org:8080/tfs/orgname/1s2ldda4-coda-4a0g-bg01-ueg59d97b086/_apis/wit/classificationNodes/Iterations/MyAppTwo/MyAppTwo_03.52HF" } ], "url": "http://tfs.orgname.org:8080/tfs/orgname/1s2ldda4-coda-4a0g-bg01-ueg59d97b086/_apis/wit/classificationNodes/Iterations/MyAppTwo" }, { "id": 113, "identifier": "caf3744b-47b9-4da5-a6d5-8d85d9943e96", "name": "DBNameCon", "structureType": "iteration", "hasChildren": true, "children": [ { "id": 114, "identifier": "ea67184f-f01f-4240-a15f-e3be3d3a00b8", "name": "Sb15", "structureType": "iteration", "hasChildren": false, "url": "http://tfs.orgname.org:8080/tfs/orgname/1s2ldda4-coda-4a0g-bg01-ueg59d97b086/_apis/wit/classificationNodes/Iterations/DBNameCon/Sb15" }, { "id": 115, "identifier": "730a3ea7-2b01-499f-9323-0f9c92058f6d", "name": "MS", "structureType": "iteration", "hasChildren": false, "url": "http://tfs.orgname.org:8080/tfs/orgname/1s2ldda4-coda-4a0g-bg01-ueg59d97b086/_apis/wit/classificationNodes/Iterations/DBNameCon/MS" } ], "url": "http://tfs.orgname.org:8080/tfs/orgname/1s2ldda4-coda-4a0g-bg01-ueg59d97b086/_apis/wit/classificationNodes/Iterations/DBNameCon" }, { "id": 300, "identifier": "32a5316b-6b9c-4ce1-9aa1-cb647e90ad40", "name": "Iteration Name", "structureType": "iteration", "hasChildren": false, "url": "http://tfs.orgname.org:8080/tfs/orgname/1s2ldda4-coda-4a0g-bg01-ueg59d97b086/_apis/wit/classificationNodes/Iterations/Iteration%20Name" }, { "id": 1448, "identifier": "a0b3871d-810a-4277-851e-52ed864b2915", "name": "_ParentApp", "structureType": "iteration", "hasChildren": true, "children": [ { "id": 1449, "identifier": "3bc8f2fb-0261-49a0-99a7-5631607ab998", "name": "FeatureModule", "structureType": "iteration", "hasChildren": true, "url": "http://tfs.orgname.org:8080/tfs/orgname/1s2ldda4-coda-4a0g-bg01-ueg59d97b086/_apis/wit/classificationNodes/Iterations/_ParentApp/FeatureModule" } ], "url": "http://tfs.orgname.org:8080/tfs/orgname/1s2ldda4-coda-4a0g-bg01-ueg59d97b086/_apis/wit/classificationNodes/Iterations/_ParentApp" } ], "_links": { "self": { "href": "http://tfs.orgname.org:8080/tfs/orgname/1s2ldda4-coda-4a0g-bg01-ueg59d97b086/_apis/wit/classificationNodes/Iterations" } }, "url": "http://tfs.orgname.org:8080/tfs/orgname/1s2ldda4-coda-4a0g-bg01-ueg59d97b086/_apis/wit/classificationNodes/Iterations" }
目标JSON
{ "options":[ { "key":"AppNameOne_03.14", "value":"App List\\AppNameOne\\AppNameOne_03.14", "enabled":false }, { "key":"AppNameOne_03.15", "value":"App List\\AppNameOne\\AppNameOne_03.15", "enabled":false }, { "key":"MyAppTwo_03.53", "value":"App List\\MyAppTwo\\MyAppTwo_03.53", "enabled":false }, { "key":"MyAppTwo_03.54", "value":"App List\\MyAppTwo\\MyAppTwo_03.54", "enabled":true }, { "key":"MyAppTwo_03.52HF", "value":"App List\\MyAppTwo\\MyAppTwo_03.52HF", "enabled":true }, { "key":"Sb15", "value":"App List\\DBNameCon\\Sb15", "enabled":true }, { "key":"MS", "value":"App List\\DBNameCon\\MS", "enabled":true }, { "key":"FeatureModule", "value":"App List\\_ParentApp\\FeatureModule", "enabled":true } ] }
JOLT转换规则
[ // 提取核心字段:根节点名称、父节点名称、当前节点名称、结束日期 { "operation": "shift", "spec": { "name": "rootName", "children": { "*": { "name": "parentNames[&1]", "children": { "*": { "name": "options[&3].key", "@(2,name)": "options[&3].parentName", "@(4,name)": "options[&3].rootName", "attributes": { "finishDate": "options[&3].finishDate" } } } } } } }, // 计算enabled字段:无结束日期则为true;有结束日期且晚于当前时间则为true,否则false { "operation": "modify-overwrite-beta", "spec": { "options": { "*": { "enabled": [ "=not(lessThan(@(1,finishDate), now()))", "=or(@(1,enabled), not(exists(@(1,finishDate))))" ] } } } }, // 拼接value字段的路径字符串 { "operation": "modify-overwrite-beta", "spec": { "options": { "*": { "value": "=join('\\\\', [@(1,rootName), @(1,parentName), @(1,key)])" } } } }, // 整理最终结构,保留需要的字段 { "operation": "shift", "spec": { "options": { "*": { "key": "options[&1].key", "value": "options[&1].value", "enabled": "options[&1].enabled" } } } } ]
规则说明
- Shift提取字段:遍历源JSON的层级结构,提取根节点名称、父节点名称、当前节点名称,以及部分节点的结束日期,为后续处理准备数据。
- Modify计算enabled:
- 若节点不存在
finishDate,直接设为true; - 若存在
finishDate,判断该日期是否晚于当前时间,是则设为true,否则设为false。
- 若节点不存在
- Modify拼接路径:将根节点、父节点、当前节点的名称用反斜杠拼接成
value字段的内容。 - Shift整理结构:过滤掉中间生成的冗余字段,只保留目标需要的
key、value、enabled三个字段,组成最终的options数组。
内容的提问来源于stack exchange,提问作者Alvin
相关产品推荐
相关产品推荐

