我有下面的 json,我只想將唯一的 product.id 過濾到陣列變數中。
我正在使用以下查詢,但結果沒有以正確的方式出現。
productIds=($(jq -r .items[].product.id $Outputfile))
實際輸出:123 123 123 456 456 null null
預期輸出:123 456
{
"documentType": "product",
"items": [
{
"ResourceId": null,
"product": {
"href": null,
"id": "123",
"mainId": "qwe345",
"primaryId": "5298"
},
"lastModifiedBy": "test",
"quantity": null,
"effectiveDate": null,
"extensions": null
},
{
"product": {
"href": null,
"id": "123",
"mainId": "qwe678",
"primaryId": "5643"
},
"lastModifiedBy": "test",
"quantity": null,
"effectiveDate": null,
"extensions": null
}
],
"createdBy": "test",
"createdOn": "2021-10-05",
"currentSeqNum": 2
}
然后我想將這些值編碼為 base64 格式,然后與一些常量值連接。我也無法與常量連接并存盤在回圈內的相同變數中。
你能幫我解決以上兩個問題嗎?
uj5u.com熱心網友回復:
您可以unique在應用陣列轉換后使用,然后join例如
productIds=$(jq -r '[.items[].product.id] | unique | join(" ")' $Outputfile)
Demo
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/391886.html
