JSON 与 jq,将所有值更改为 *

JSON 与 jq,将所有值更改为 *

我需要用 * 替换 JSON 文件中的每个值,然后将输出返回到新的 JSON 文件。

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "Global": {
    "Version": "1.0.0",
    "Environment": {
      "Long": "Local",
      "Short": "loc"
    },
    "ActiveDirectory": {
      "Role": {
        "User": "user",
        "Manager": "manager",
        "Admin": "admin"
      }
    },
    "Jwt": {
      "Authority": "http://localost:8089/auth/reals/master",
      "Audience": "pneumanet-client",
      "AuthorizationUrl": "http://localost:8089/auth/reals/master/protocol/openid-connect/auth",
      "TokenUrl": "http://localost:8089/auth/reals/master/protocol/openid-connect/token",
      "Real": "master"
    }
  }
}

正如您所看到的所有具有“默认”之类值的键:“信息”我想将“信息”更改为*。这必须应用于所有键值字段。这对于 jq 来说可能吗,因为我无法弄清楚。谢谢。

答案1

一种可能的方法是使用递归下降(..)并选择具有值的节点"Information"并更新它们:

$ jq '(.. | select(. == "Information")) |= "*"' file.json
{
  "Logging": {
    "LogLevel": {
      "Default": "*",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "*"
    }
  },
  "Global": {
    "Version": "1.0.0",
    "Environment": {
      "Long": "Local",
      "Short": "loc"
    },
    "ActiveDirectory": {
      "Role": {
        "User": "user",
        "Manager": "manager",
        "Admin": "admin"
      }
    },
    "Jwt": {
      "Authority": "http://localost:8089/auth/reals/master",
      "Audience": "pneumanet-client",
      "AuthorizationUrl": "http://localost:8089/auth/reals/master/protocol/openid-connect/auth",
      "TokenUrl": "http://localost:8089/auth/reals/master/protocol/openid-connect/token",
      "Real": "master"
    }
  }
}

相关内容