使用JOLT转换JSON并添加locations数组遇问题求助
JOLT转换问题解决:添加完整locations数组并重命名字段
问题背景
输入JSON:
{ "data": { "tests": [ { "id": 304674, "name": "Test Number One", "description": "First Test" }, { "id": 12345, "name": "Test Number Two", "description": "Second test" } ] } }
期望输出:
{ "data": { "tests": [ { "TestID": 304674, "Name": "Test Number One", "Description": "First Test", "locations": [ "Paris", "London" ] }, { "id": 12345, "name": "Test Number Two", "description": "Second test", "locations": [ "Paris", "London" ] } ] } }
原JOLT规范运行后,locations数组仅显示"Paris",需修正该问题。
正确的JOLT规范
方案1:统一重命名字段+添加完整数组
[ { "operation": "shift", "spec": { "data": { "tests": { "*": { "id": "data.tests[&1].TestID", "name": "data.tests[&1].Name", "description": "data.tests[&1].Description" } } } } }, { "operation": "modify-default-beta", "spec": { "data": { "tests": { "*": { "locations": "= ['Paris', 'London']" } } } } } ]
方案2:匹配期望输出(仅第一个元素重命名)
[ { "operation": "shift", "spec": { "data": { "tests": { "0": { "id": "data.tests[&1].TestID", "name": "data.tests[&1].Name", "description": "data.tests[&1].Description" }, "1": { "*": "data.tests[&1].&" } } } } }, { "operation": "modify-default-beta", "spec": { "data": { "tests": { "*": { "locations": "= ['Paris', 'London']" } } } } } ]
问题原因及解释
原modify-default-beta中直接写"locations": ["Paris", "London"]时,JOLT会将数组视为键值对结构处理,而非直接赋值整个数组,最终仅保留单个元素。
通过添加=操作符,可明确告知JOLT将右侧数组作为整体赋值给locations字段,从而完整保留数组内容。
内容的提问来源于stack exchange,提问作者Michael Wyatt
相关产品推荐
相关产品推荐

