[英]JOLT transformation split object in array to two objects
我有對象數組,我想將此數組中的每個 object 拆分為兩個對象,我的輸入如下所示:
[
{
"amount": 30,
"currency": "USD",
"status": "Approved",
"timestamp": "1660117356314",
"user_id": 123,
"balance_after": 30.03,
"transaction_id": 123
},
{
"amount": 10,
"currency": "USD",
"status": "Approved",
"timestamp": "1660117356314",
"user_id": 124,
"balance_after": 70.03,
"transaction_id": 124
}
]
我正在嘗試這樣的Jolt Spec :
[
{
"operation": "shift",
"spec": {
"*": "[0].&",
"@(1,timestamp)": "[1].timestamp",
"@(1,user_id)": "[1].user_id",
"@(1,balance_after)": "[1].balances.[0].balance",
"@(1,currency)": "[1].balances.[0].currency",
"#real_money": "[1].balances.[0].key",
"#0": "[2].balances.[1].balance",
"@(1,currency)": "[2].balances.[1].currency",
"#bonus_money": "[2].balances.[1].key"
}
}
]
我的預期結果是
[
{
"amount": 30,
"currency": "USD",
"status": "Approved",
"timestamp": "1660117356314",
"user_id": 123,
"balance_after": 30.03,
"transaction_id": 123
},
{
"timestamp": "1660117356314",
"user_id": 123,
"balances": [
{
"balance": 30.03,
"currency": "USD",
"key": "real_money"
},
{
"balance": 0,
"currency": "USD",
"key": "bonus_money"
}
]
},
{
"amount": 10,
"currency": "USD",
"status": "Approved",
"timestamp": "1660117356314",
"user_id": 124,
"balance_after": 70.03,
"transaction_id": 124
},
{
"timestamp": "1660117356314",
"user_id": 124,
"balances": [
{
"balance": 70.03,
"currency": "USD",
"key": "real_money"
},
{
"balance": 0,
"currency": "USD",
"key": "bonus_money"
}
]
}
]
但我收到了完全不同的結果,這個規范適用於這個輸入:
{
"amount": 30,
"currency": "USD",
"status": "Approved",
"timestamp": "1660117356314",
"user_id": 123,
"balance_after": 30.03,
"transaction_id": 123
}
我收到:
[
{
"amount": 30,
"currency": "USD",
"status": "Approved",
"timestamp": "1660117356314",
"user_id": 123,
"balance_after": 30.03,
"transaction_id": 123
},
{
"timestamp": "1660117356314",
"user_id": 123,
"balances": [
{
"balance": 30.03,
"key": "real_money"
},
{
"currency": "USD",
"balance": "0",
"key": "bonus_money"
}
]
}
]
但REAL_MONEY余額沒有顯示貨幣
這里有沒有人有解決方案如何實現目標?
當您的輸入是一個數組時,您可以使用它。
[
{
"operation": "shift",
"spec": {
"*": {
"*": "[#2].first.&",
"timestamp": ["[#2].first.&", "[#2].second.timestamp"],
"user_id": ["[#2].first.&", "[#2].second..user_id"],
"balance_after": ["[#2].first.&", "[#2].second.balances[0].balance"],
"1,currency": ["[#2].first.&", "[#2].second.balances[0].currency"],
"#real_money": "[#2].second.balances[0].key",
"#0": "[#2].second.balances[1].balance",
"currency": ["[#2].first.&", "[#2].second.balances[1].currency"],
"#bonus_money": "[#2].second.balances[1].key"
}
}
},
{
"operation": "sort",
"spec": {
"*": ""
}
},
{
"operation": "shift",
"spec": {
"*": {
"first|second": ""
}
}
}
]
您可以使用這些規格
[
{
// generate attributes which will be nested within "balances" array
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"new_user_id": "=(@(1,user_id))",
"new_timestamp": "=(@(1,timestamp))",
"bal_balance": "=(@(1,balance_after))",
"bal_currency": "=(@(1,currency))"
}
}
},
{
// distinguish outermost objects and arrays
"operation": "shift",
"spec": {
"*": {
"*": "@(1,user_id).&",
"new_*": "n[&1].&(0,1)",
"bal_*": "n[&1].balances[0].&(0,1)",
"#real_money": "n[&1].balances[0].key", //static content
"#0": "n[&1].balances[1].balance", //static content
"#USD": "n[&1].balances[1].currency", //static content
"#bonus_money": "n[&1].balances[1].key" //static content
}
}
},
{
"operation": "shift",
"spec": {
"n": {
"*": {
"@": "&1"
}
},
"*": {
"@": "o&1"
}
}
},
{
// get rid of key names of the outermost objects
"operation": "shift",
"spec": {
"*": ""
}
},
{
"operation": "sort"
}
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.