簡體   English   中英

jq - 如何有條件地過濾掉對象

[英]jq - how to conditionally filter out objects

我正在嘗試極大地簡化大型 JSON 文件,該過程的一部分是有條件地過濾掉一些對象。 我有一系列我想申請的條件,但我什至無法讓一個條件正常工作。

我一直在使用 jq 文檔,但我不是專業開發人員,因此閱讀它具有挑戰性。 我在另一個 stackoverflow 線程中找到了一個這樣的例子,所以我一直在努力適應它。

我的源 JSON 文件雖然很大,但干凈且格式良好。 它基本上是一組平面對象。 對象中的一些鍵也是數組,但無論如何我都會丟棄其中的大部分。 下面是一個示例,顯示了第一個對象:

[
   {
      "object":"card",
      "id":"86bf43b1-8d4e-4759-bb2d-0b2e03ba7012",
      "oracle_id":"0004ebd0-dfd6-4276-b4a6-de0003e94237",
      "multiverse_ids":[
         15862
      ],
      "mtgo_id":15870,
      "mtgo_foil_id":15871,
      "tcgplayer_id":3094,
      "name":"Static Orb",
      "lang":"en",
      "released_at":"2001-04-11",
      "uri":"https://api.scryfall.com/cards/86bf43b1-8d4e-4759-bb2d-0b2e03ba7012",
      "scryfall_uri":"https://scryfall.com/card/7ed/319/static-orb?utm_source=api",
      "layout":"normal",
      "highres_image":true,
      "mana_cost":"{3}",
      "cmc":3.0,
      "type_line":"Artifact",
      "oracle_text":"As long as Static Orb is untapped, players can't untap more than two permanents during their untap steps.",
      "colors":[

      ],
      "color_identity":[

      ],
      "legalities":{
         "standard":"not_legal",
         "future":"not_legal",
         "historic":"not_legal",
         "pioneer":"not_legal",
         "modern":"not_legal",
         "legacy":"legal",
         "pauper":"not_legal",
         "vintage":"legal",
         "penny":"not_legal",
         "commander":"legal",
         "brawl":"not_legal",
         "duel":"legal",
         "oldschool":"not_legal"
      },
      "games":[
         "paper",
         "mtgo"
      ],
      "reserved":false,
      "foil":true,
      "nonfoil":true,
      "oversized":false,
      "promo":false,
      "reprint":true,
      "variation":false,
      "set":"7ed",
      "set_name":"Seventh Edition",
      "set_type":"core",
      "collector_number":"319",
      "digital":false,
      "rarity":"rare",
      "flavor_text":"The warriors fought against the paralyzing waves until even their thoughts froze in place.",
      "card_back_id":"0aeebaf5-8c7d-4636-9e82-8c27447861f7",
      "artist":"Terese Nielsen",
      "artist_ids":[
         "eb55171c-2342-45f4-a503-2d5a75baf752"
      ],
      "illustration_id":"6f8b3b2c-252f-4f95-b621-712c82be38b5",
      "border_color":"white",
      "frame":"1997",
      "full_art":false,
      "textless":false,
      "booster":true,
      "story_spotlight":false,
      "edhrec_rank":1836,
      "prices":{
         "usd":"17.07",
         "usd_foil":"72.02",
         "eur":"10.73",
         "tix":"0.79"
      },
      "related_uris":{
         "gatherer":"https://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=15862",
         "tcgplayer_decks":"https://decks.tcgplayer.com/magic/deck/search?contains=Static+Orb\u0026page=1\u0026utm_campaign=affiliate\u0026utm_medium=api\u0026utm_source=scryfall",
         "edhrec":"https://edhrec.com/route/?cc=Static+Orb",
         "mtgtop8":"https://mtgtop8.com/search?MD_check=1\u0026SB_check=1\u0026cards=Static+Orb"
      }
   }
]

首先,我試圖添加一個條件,該條件將刪除鍵“reprint”的值為 true 的對象。 這是我到目前為止所得到的:

curl -s https://archive.scryfall.com/bulk-data/default-cards/default-cards-20200529170427.json | jq .[] - "map(select(.reprint[] | contains (\\"true\\")))" > reprints_removed.json

但我不斷收到此錯誤:

jq: error: Could not open file map(select(.reprint[] | contains ("true"))): No such file or directory

一旦我開始工作,我想開始使用復合條件。 因此,例如,接下來我將刪除鍵“reprint”為“true”或鍵“border_color”為“silver”的對象。

我哪里錯了?

頂級數組中每個對象中 .reprint 的值是嚴格的布爾值,通過運行可以看出:

jq 'map(.reprint)|unique'

因此,選擇 .reprint 為false的對象的有效方法是使用過濾器:

map(select(.reprint == false))

這將保留數組結構。 如果您只想將 JSON 對象作為流,您可以編寫:

.[] | select(.reprint == false)

由於 .reprint 是嚴格的布爾值,因此有各種等效的條件公式。

否定

如果 .reprint 是多值的,那么要排除.reprint == true的對象,您可以使用否定:

.[] | select(.reprint == true | not)

請注意,在 jq 中, not是一個 0 元過濾器——它沒有參數。

復合條件

刪除鍵“reprint”為“true”或鍵“border_color”為“silver”的對象。

map(select( (.reprint == true or .border_color == "silver") | not))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM