如何使用变量用新值替换 json 文件中的值

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

我想用一个新值替换 "id" 的值。 我有以下 json 文件。

 [
  {
    "adminConsentDescription": "default",
    "adminConsentDisplayName": "default",
    "id": "5ad89ced-56a1-2222-8200-1ded5129d788",
    "isEnabled": true,
    "type": "User",
    "userConsentDescription": null,
    "userConsentDisplayName": null,
    "value": "default"
  }
]

并想用变量app_id替换它

app_id=6e663913-0000-3456-c083e40fbc99
输出应该是

[
  {
    "adminConsentDescription": "default",
    "adminConsentDisplayName": "default",
    "id": "6e663913-0000-3456-c083e40fbc99",
    "isEnabled": true,
    "type": "User",
    "userConsentDescription": null,
    "userConsentDisplayName": null,
    "value": "default"
  }
]

我尝试并达到了能够使用 Jq 提取价值的地步

先定义变量

App_id=6e663913-0000-3456-c083e40fbc99

jq --argjson id $app_id 'map(."id" = $app_id)' oauth2-permissions.json

尝试了几个其他排列,但无法用变量替换。有什么想法吗?

bash jq
1个回答
1
投票

您可以使用:

APP_ID=6e663913-0000-3456-c083e40fbc99
jq --arg app_id "$APP_ID" '.[].id = $app_id' oauth2-permissions.json
© www.soinside.com 2019 - 2024. All rights reserved.