簡體   English   中英

Jq 替換 json 中的一個詞

[英]Jq to replace a word with in the json

我在下面有一個 json,我需要在數據部分獲取鍵“toModify”的值。 並且還想將 key(toModify) 值修改為另一個值,例如:hello1234567 到 xyz。 我們如何使用 jq 做到這一點。

{
  "items": [
    {
      "source": {
        "id": "12334"        
      },
      "data": {
        "name": "test",
        "value": "test",        
        "cData": [
          {
            "key": "keOne",
            "value": "hello"
          },
          {
            "key": "toModify",
            "value": "hello1234567"
          }
        ]
      } 
    }
  ]
}

您的輸入,特別是cData鍵的值,看起來是為與to_entriesfrom_entries一起使用而量身定制的。

$ jq '.items[0].data.cData |= (from_entries | .toModify = "xyz" | to_entries)' tmp.json
{
  "items": [
    {
      "source": {
        "id": "12334"
      },
      "data": {
        "name": "test",
        "value": "test",
        "cData": [
          {
            "key": "keOne",
            "value": "hello"
          },
          {
            "key": "toModify",
            "value": "xyz"
          }
        ]
      }
    }
  ]
}

要求有點粗略,但在這種特殊情況下,以下內容會產生所需的結果,並說明了一種方法:

.items[].data.cData
  |= map(if .key == "toModify" then .key = "MODIFIED" else . end)

暫無
暫無

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

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