我希望能夠使用jq
在輸入JSON中輸出2個數組的'產品'...例如,給定下列輸入JSON:如何使用jq生成輸入中存在的兩個數組的笛卡爾乘積JSON
{
"quantities": [
{
"product": "A",
"quantity": 30
},
{
"product": "B",
"quantity": 10
}
],
"portions": [
{
"customer": "C1",
"percentage": .6
},
{
"customer": "C2",
"percentage": .4
}
]
}
我想產生以下輸出(或類似...):
[
{
"customer": "C1",
"quantities": [
{
"product": "A",
"quantity": 18
},
{
"product": "B",
"quantity": 6
}
]
},
{
"customer": "C2",
"quantities": [
{
"product": "A",
"quantity": 12
},
{
"product": "B",
"quantity": 4
}
]
}
]
因此,換句話說,對於每一部分,使用它的百分比的值並將其應用於每個產品數量。鑑於2只及2部應該產生4個結果..給3只及2部應該產生6個結果,等...
我使用foreach
過濾器已經做了一些嘗試,但都無濟於事......