我正在嘗試 curl 一些回傳 json 檔案的 URL,如下所示
[
10,
20,
30,
40,
]
現在我正在嘗試將這些值保存在檔案中,并為其分配一個變數。
需要在檔案中輸出為
a=10
b=20
c=30
d=40
提前致謝。
uj5u.com熱心網友回復:
您可以使用implode陣列索引創建字母,該索引在應用于陣列.key時提供。to_entries
jq -r 'to_entries[] | "\([.key 97] | implode)=\(.value)"'
a=10
b=20
c=30
d=40
演示
要提供單個名稱,您可以引入一組名稱并用于transpose對齊:
jq -r '
[["Price", "Amount", "Value", "Tax"], .]
| transpose[] | "\(first)=\(last)"
'
Price=10
Amount=20
Value=30
Tax=40
演示
注意:該串列也可以從 jq 外部提供:
- 使用 JSON 陣列
jq -r --argjson names '["Price", "Amount", "Value", "Tax"]' \
'[$names, .] | transpose[] | "\(first)=\(last)"'
- 使用位置引數
jq -r '[$ARGS.positional, .] | transpose[] | "\(first)=\(last)"' \
--args Price Amount Value Tax
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/510065.html
標籤:数组jsonjq
