我有一個這樣的嵌套 JSON 物件:
{
"0":{
"testone":72,
"testtwo":1
},
"1":{
"testone":72,
"testtwo":1
},
"2":{
"testone":72,
"testtwo":1
}
}
我想轉換為:
[
{
"one":72,
"two":1
},
{
"one":72,
"two":1
},
{
"one":72,
"two":1
}
]
我如何使用 JOLT 實作這一目標?感謝您的投入。
uj5u.com熱心網友回復:
您可以使用以下規格
[
//exchange key and values
{
"operation": "shift",
"spec": {
"*": {
"*": {
"$": "[&2].@(0)"
}
}
}
},
//split the values by the prefix "test"
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": "=split('test',@(1,&))"
}
}
},
//get rid of the prefixes "test"
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": "=join('',@(1,&))"
}
}
},
//re-exchange key and values
{
"operation": "shift",
"spec": {
"*": {
"*": {
"$": "[&2].@(0)"
}
}
}
}
]

編輯(由于您的評論):不幸的是,沒有直接重命名方法,但可以通過使用以下方法單獨撰寫每個鍵值對來提供解決方法
[
//rename innermost key names
{
"operation": "shift",
"spec": {
"*": {
"testone": "&1.one",
"testtwo": "&1.two"
}
}
},
//get rid of the object keys
{
"operation": "shift",
"spec": {
"*": ""
}
}
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359708.html
