簡體   English   中英

使用 jq 替換 json 中的爭論

[英]replace arguements in a json using jq

{
"containers": [
          {
            "args": [
              "water",
              "fight",
              "--homein",
              "$(POD_NAMESPACE).svc.cluster.local",
              "--proxyLogLevel=warning",
              "--proxyComponentLogLevel=misc:error",
              "--log_output_level=default:info",
              "ms-madison",
              "--trust-domain=cluster.local"
            ]}]
}

我可以用丑陋的解決方案 .containers[0].args[8] |= "mr-harmison"' 用 mr-harmison 替換 ms-madison

ms-madison 可以出現在任何位置。 你能給我建議一個更好的方法來處理這個問題嗎?

一個平凡但強大的解決方案:

(.containers[0].args | index("ms-madison")) as $ix
| if $ix then .containers[0].args[$ix] = "mr-harmison" else . end

首先,這是一個解決方案,它與“ms-madison”出現的位置完全無關,但會改變目標字符串的每次出現:

walk(if . == "ms-madison" then "mr-harmison" else . end)

其次,如果只想更改最多一次:

. as $in
| (first(paths | select(. as $p | $in | getpath($p) == "ms-madison")) // null) as $p
| if $p then setpath($p; "mr-harmison") else . end

限制對特定上下文的更改

這是如何“走” .containers以更改作為每個數組值“args”鍵的頂級元素出現的所有目標字符串的示例:

.containers |= walk( if type=="object"
                        and (.args|type)=="array"
                     then .args
                        |= map_values(
                             if . == "ms-madison" 
                             then "mr-harmison"
                             else . end)
                     else . end)

暫無
暫無

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

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