如何在JQ中添加变量

发布于 2025-02-11 20:15:42 字数 1105 浏览 2 评论 0原文

我正在尝试在从JSON文件中获取信息时添加一个变量,如下所示。

stack=$(cat profiles.json | jq '.generic.category')

email=$(cat profiles.json | jq '.central.[Need to add $stack variable here].email')
echo $email
password=$(cat profiles.json | jq '.central.[Need to add $stack variable here].password')
echo $password

我尝试了一些事情,例如jq -arg v $ stack'.central [$ v] password*',但它不起作用。

的样子。

  "central": {
        "one": {
            "tenant": "xxx-yyy-zzz",
            "email": "[email protected]",
            "password": "1111"
         },
        "two": {
            "tenant": "aaa-bbb-ccc",
            "email": "[email protected]",
            "password": "2222"
         }
  },
  "generic": {
        "username": "root",
        "password": "xyz",
        "project": "ABC",
        "category": "two"
    }

这就是我的个人资料

I'm trying to add a variable while getting the info from a JSON file as shown below.

stack=$(cat profiles.json | jq '.generic.category')

email=$(cat profiles.json | jq '.central.[Need to add $stack variable here].email')
echo $email
password=$(cat profiles.json | jq '.central.[Need to add $stack variable here].password')
echo $password

I tried few things like jq --arg v $stack '.central[$v]password*' but it didnt work.

This is what my the profiles.json looks like:

  "central": {
        "one": {
            "tenant": "xxx-yyy-zzz",
            "email": "[email protected]",
            "password": "1111"
         },
        "two": {
            "tenant": "aaa-bbb-ccc",
            "email": "[email protected]",
            "password": "2222"
         }
  },
  "generic": {
        "username": "root",
        "password": "xyz",
        "project": "ABC",
        "category": "two"
    }

What is the right command to fetch the required information using the variable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒绊 2025-02-18 20:15:42
$ cat profiles.json
{
   "central": {
        "one": {
            "tenant": "xxx-yyy-zzz",
            "email": "[email protected]",
            "password": "1111"
         },
        "two": {
            "tenant": "aaa-bbb-ccc",
            "email": "[email protected]",
            "password": "2222"
         }
  },
  "generic": {
        "username": "root",
        "password": "xyz",
        "project": "ABC",
        "category": "two"
    }
}

$ jq --arg MYVAR one '.central[$MYVAR].password' profiles.json
"1111"
$ cat profiles.json
{
   "central": {
        "one": {
            "tenant": "xxx-yyy-zzz",
            "email": "[email protected]",
            "password": "1111"
         },
        "two": {
            "tenant": "aaa-bbb-ccc",
            "email": "[email protected]",
            "password": "2222"
         }
  },
  "generic": {
        "username": "root",
        "password": "xyz",
        "project": "ABC",
        "category": "two"
    }
}

$ jq --arg MYVAR one '.central[$MYVAR].password' profiles.json
"1111"
野の 2025-02-18 20:15:42

您可以在没有Shell变量stack的情况下执行,除非您想在Shell脚本的以后在不同的位置使用它。

email=$(jq -r '.central[.generic.category].email' profiles.json)
echo $email
password=$(jq -r '.central[.generic.category].password' profiles.json)
echo $password

You can do without the shell variable stack, unless you want to use it in different places later in your shell script.

email=$(jq -r '.central[.generic.category].email' profiles.json)
echo $email
password=$(jq -r '.central[.generic.category].password' profiles.json)
echo $password
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文