如何使用现有对象中的 json 数据来更新其他对象?

问题描述 投票:0回答:1

这是我的第一个输入(记录为 bash var $services)

{
  "key": "movies-wikibase-jobrunner",
  "subPath": "LocalSettings.php",
  "cm": "movies-wikibase-jobrunner-localsettings-override-php"
}
{
  "key": "movies-wikibase-wdqs-frontend",
  "subPath": "wdqs_front_index.html",
  "cm": "movies-wikibase-wdqs-frontend-index-html"
}

我想使用subPath来更新deployment.json。 我尝试将 subPath 作为 bash 参数传递给 jq 但没有任何效果

cat deployment.json | jq -r --arg services $services '((.spec.template.spec.containers[].volumeMounts[]? )) |= {mountPath: .mountPath, name: (.key, subPath: $services|select(.key=="movies-wikibase-wdqs-frontend").subPath})'

我怎样才能实现这一目标,这是好方法吗?

json jq
1个回答
0
投票
jq --argjson services "$services" '
  .spec.template.spec.containers[].volumeMounts[] |=
    (
      .name as $name |
      . + { mountPath: ($services[] | select(.key == $name).subPath // .mountPath) }
    )
' deployment.json > updated_deployment.json
© www.soinside.com 2019 - 2024. All rights reserved.