簡體   English   中英

如何使用 jq 替換 json 文件中的值並返回全部內容

[英]How to replace a value in json file using jq and returning the whole content

我有一個這樣的 json

{
  "AgentGroupId": null,
  "AgentId": null,
  "CreateType": "Website",
  "IsPrimary": true,
  "IsShared": true,
  "HeaderAuthentication": {
    "Headers": [
      {
        "Name": "api-key",
        "Value": "TEST_API_KEY_VALUE-2",
        "OriginalName": null,
        "IsReplacedCredentials": false
      },
      {
        "Name": "Authorization",
        "Value": "",
        "OriginalName": null,
        "IsReplacedCredentials": false
      }
    ],
    "IsEnabled": true
  },
  "IsTimeWindowEnabled": false,
  "AdditionalWebsites": [],
  "BasicAuthenticationApiModel": {
    "Credentials": null,
    "IsEnabled": false,
    "NoChallenge": false
  },
  "ClientCertificateAuthenticationSetting": null,
  "Cookies": null,
  "CrawlAndAttack": true,
  "EnableHeuristicChecksInCustomUrlRewrite": true,
  "ExcludedLinks": [
    {
      "RegexPattern": "gtm\\.js"
    },
    {
      "RegexPattern": "WebResource\\.axd"
    },
    {
      "RegexPattern": "ScriptResource\\.axd"
    }
  ],
  "ExcludedUsageTrackers": [],
  "DisallowedHttpMethods": [],
  "ExcludeLinks": true,
  "ExcludeAuthenticationPages": false,
  "FindAndFollowNewLinks": true,
  "FormAuthenticationSettingModel": {
    "Integrations": {},
    "CustomScripts": [],
    "InteractiveLoginRequired": false,
    "DefaultPersonaValidation": null,
    "DetectBearerToken": true,
    "DisableLogoutDetection": false,
    "IsEnabled": false,
    "LoginFormUrl": null,
    "LoginRequiredUrl": null,
    "LogoutKeywordPatterns": null,
    "LogoutKeywordPatternsValue": null,
    "LogoutRedirectPattern": null,
    "OverrideTargetUrl": false,
    "Personas": [],
    "PersonasValidation": null
  }
}

我的目標是替換HeaderAuthentication下的api-key的值(它可以在任何索引中,0、2、1,...)

我做了這個

jq '.HeaderAuthentication.Headers[] | select(.Name == "api-key") | .Value = "xxx"' scanprofile.json > tmp && mv tmp scanprofile.json

問題是jq似乎只返回替換的部分,但我需要整個文件,我做錯了什么?

這是運行命令后文件的內容

{
  "Name": "api-key",
  "Value": "xxx",
  "OriginalName": null,
  "IsReplacedCredentials": false
}

附言。 我看到一些使用海綿的 stackoverflow 帖子,我不能在我們的環境中使用海綿

將您的過濾器表達式放在(..)中,這意味着它將從根應用於節點結構,而不是單獨.Headers[]中。 在括號下,使用更新分配|=或正常分配使其工作。

( .HeaderAuthentication.Headers[] | select(.Name == "api-key") | .Value ) |= "xxx"

暫無
暫無

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

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