如何使用JOLT转换将键从嵌套JSON对象移动到上一级



我想做一个JOLT转换,并在一个嵌套的JSON对象内移动键到upllevel。

这是我的输入JSON:
[
{
"deviceNumber": "1287391731_city",
"country": {
"State": [
{
"Priority": "MEDIUM",
"City": {
"Town": [
{
"unit": "",
"type": "",
"value": "Mumbai",
"key": "District1"
},
{
"unit": "",
"type": "",
"value": "Delhi",
"key": "District2"
},
{
"unit": "",
"type": "",
"value": "2",
"key": "DistrictCount"
}
]
},
"description": "describe the subcategory"
}
]
},
"location": "8.21311,76.018231"
},
{
"deviceNumber": "1287391731_city",
"country": {
"State": [
{
"Priority": "MEDIUM",
"City": {
"Town": [
{
"unit": "",
"type": "",
"value": "Bangalore",
"key": "District1"
},
{
"unit": "",
"type": "",
"value": "Chennai",
"key": "District2"
},
{
"unit": "",
"type": "",
"value": "2",
"key": "DistrictCount"
}
]
},
"description": "describe the subcategory"
}
]
},
"location": "8.21311,76.018231"
},
{
"deviceNumber": "1287391731_city",
"country": {
"State": [
{
"Priority": "MEDIUM",
"City": {
"Town": [
{
"unit": "",
"type": "",
"value": "Ahmedabad",
"key": "District1"
},
{
"unit": "",
"type": "",
"value": "Kolkata",
"key": "District2"
},
{
"unit": "",
"type": "",
"value": "2",
"key": "DistrictCount"
}
]
},
"description": "describe the subcategory"
}
]
},
"location": "8.21311,76.018231"
}
]

这是期望的输出:

[
{
"@context": "https://my_context.in",
"type": [
"CITYVIEW"
],
"deviceNumber": "1287391731_city",
"DistrictCount": 2,
"location": "8.21311,76.018231",
"District1": {
"NormalValue": "Delhi"
},
"District2": {
"NormalValue": "Mumbai"
}
},
{
"@context": "https://my_context.in",
"type": [
"CITYVIEW"
],
"deviceNumber": "1287391731_city",
"DistrictCount": 2,
"location": "8.21311,76.018231",
"District1": {
"NormalValue": "Bangalore"
},
"District2": {
"NormalValue": "Chennai"
}
},
{
"@context": "https://my_context.in",
"type": [
"CITYVIEW"
],
"deviceNumber": "1287391731_city",
"DistrictCount": 2,
"location": "8.21311,76.018231",
"District1": {
"NormalValue": "Ahmedabad"
},
"District2": {
"NormalValue": "Kolkata"
}
}
]

这里districtCount被上移,值显示为整数。类型和上下文是默认值,可以使用默认操作创建。可以使用以下规格转移区t1和区t2:

[
{
"operation": "shift",
"spec": {
"*": {
"country": {
"*": {
"*": {
"*": {
"*": ""
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"value": "@(1,key).NormalValue"
}
}
}
]

这是我得到的输出:

{
"District1": {
"NormalValue": "Mumbai"
},
"District2": {
"NormalValue": "Delhi"
},
"DistrictCount": {
"NormalValue": "2"
}
}

你能帮我写一下关于JOLT转换的规范以得到预期的输出吗?

你可以使用这个规范:

[
{
"operation": "shift",
"spec": {
"*": {
"#https://my_context.in": "[&1].\@context",
"*": "[&1].&",
"type": "[&1].&[]",
"country": {
"*": {
"*": {
"*": {
"*": {
"*": {
"*": {
"DistrictCount": {
"@(2,value)": "[&9].@2"
},
"District*": {
"@(2,value)": "[&9].@2.NormalValue"
}
}
}
}
}
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"DistrictCount": "=toInteger"
}
}
}
]

最新更新