這是一些示例資料: https ://gist.github.com/dmc2015/19fc41aa677ba02a3bc49c53057ce408
我在下面的每個腳本都應該可以作業,但我不知道如何組合它們。我需要執行以下操作:
- 更改鍵名
- 能夠選擇特定的鍵回傳,我不需要全部
- 能夠選擇和決議值
- 將決議的值轉換為鍵值對并將它們添加回 json
更改鍵名
jq '.cards[] | select(.closed == false) | with_entries(if .key == "id" then .key = "trello_id" else . end) | .' data.json
回傳:
{
"trello_id": "1234",
...
..
}
選擇要回傳的特定鍵
jq '.cards[] | with_entries(select([.key] | inside(["name", "description"]) ) ) | .' data.json
回傳:
{
"desc": "*Important Notes* ",
"name": "Housing - Lawrence"
}
查詢和決議值以創建我自己的密鑰
jq '.cards[] |
.checklists[] | select(.name == "Acct Information") |
.checkItems[] | select(.name | contains ("Location")) |
.name | .' data.json |
cut -d ":" -f 1 | sed 's/"//g' | sed -e 's/\(.*\)/\L\1/' | sed -r 's/\s /_/g'
回傳:location_address
jq '.cards[] | .checklists[] | select(.name == "Acct Information") |
.checkItems[] | select(.name | contains ("Location")) |
.name | .' data.json |
cut -d ":" -f 2 |
sed -e 's/\"//g' -e 's/*//g'
回傳"123 Hopscotch Way"
我可以單獨完成大部分作業,但我正在努力尋找一種在一個查詢中完成所有作業的方法。這接近我想要的最終結果。
[
{
"trello_id": "1234",
"desc": "*Important Notes* ",
"name": "Housing - Lawrence"
"location_address": "123 Hopscotch Way"
}
]
uj5u.com熱心網友回復:
像這樣的東西可能是您正在尋找的東西嗎?
.cards | map(
select(.closed == false) | {trello_id: .id, desc, name} ([
.checklists[] | select(.name == "Acct Information")
| .checkItems[].name | select(contains("Location"))
| capture("(?<key>[^:]*):\\s*(?<value>.*)")
| .key |= (gsub("\\s "; "_") | ascii_downcase)
| .value |= gsub("\\*"; "")
] | from_entries)
)
[
{
"trello_id": "1234",
"desc": "*Important Notes* ",
"name": "Housing - Lawrence",
"location_address": "123 Hopscotch Way"
}
]
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/525707.html
標籤:json重击sedjq等
下一篇:通過shell腳本注冊別名
