需要帮助在数组内创建对象

发布于 2025-01-09 17:44:27 字数 1331 浏览 1 评论 0原文

jq 查询如下,

jq -n  --arg cname "$1" --arg dns "$2" '
  {
    "extra_vars": {
      "oper_tasks": [ "records" ],
      "zone_info": [
        {
          "zone": "exmple.com",
          "records": [
            {
              "name": $cname,
              "type": "CNAME",
              "value": $dns,
              "ttl": 3600
            }
          ]
        }
      ]
    }
  }
'

上面的 jq cn 产生类似这样的内容,当我们作为脚本执行时,cname 和 dns 可以填充参数。

"extra_vars": {
    "oper_tasks": [
      "records"
    ],
    "zone_info": [
      {
        "zone": "exmple.com",
        "records": [
          {
            "name": "",
            "type": "CNAME",
            "value": "",
            "ttl": 3600
          }
        ]
      }
    ]
  }
}

我们如何从一个输入文件创建多个如下所示的对象。就像我们在 for 循环中所做的那样。

"extra_vars": {
"oper_tasks": [
  "records"
],
"zone_info": [
  {
    "zone": "exmple.com",
    "records": [
      {
        "name": <cname1 from input file>,
        "type": "CNAME",
        "value": "",
        "ttl": 3600
      },
      {
        "name": <cname2 from input file>,
        "type": "CNAME",
        "value": "",
        "ttl": 3600
      }
    ]
  }
]

}

jq query as like this ,

jq -n  --arg cname "$1" --arg dns "$2" '
  {
    "extra_vars": {
      "oper_tasks": [ "records" ],
      "zone_info": [
        {
          "zone": "exmple.com",
          "records": [
            {
              "name": $cname,
              "type": "CNAME",
              "value": $dns,
              "ttl": 3600
            }
          ]
        }
      ]
    }
  }
'

The above jq cn produce something like this , cname and dns can be filled with arguments when we execute as script.

"extra_vars": {
    "oper_tasks": [
      "records"
    ],
    "zone_info": [
      {
        "zone": "exmple.com",
        "records": [
          {
            "name": "",
            "type": "CNAME",
            "value": "",
            "ttl": 3600
          }
        ]
      }
    ]
  }
}

how we can create more than one objects like below from an input file. something like how we do in for loop.

"extra_vars": {
"oper_tasks": [
  "records"
],
"zone_info": [
  {
    "zone": "exmple.com",
    "records": [
      {
        "name": <cname1 from input file>,
        "type": "CNAME",
        "value": "",
        "ttl": 3600
      },
      {
        "name": <cname2 from input file>,
        "type": "CNAME",
        "value": "",
        "ttl": 3600
      }
    ]
  }
]

}

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

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

发布评论

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

评论(1

国粹 2025-01-16 17:44:27

想象一下,您有一个文件 cnames.txt,其中每一行都包含一个 CNAME 域,后跟其值:

foo.example.com google.com
bar.example.com stackoverflow.com

让我们创建该文件:

cat <<EOF > cnames.txt
foo.example.com google.com
bar.example.com stackoverflow.com
EOF

您可以将一个 jq“嵌套”到其他可以使用jq 手册中描述的--slurpfile。以下命令应该执行您想要的操作:

cat cnames.txt | xargs -n2 jq -n '{
    "name": $ARGS.positional[0],
    "type": "CNAME",
    "value": $ARGS.positional[1],
    "ttl": 3600
}' --args | jq -n --slurpfile records /dev/stdin '
  {
    "extra_vars": {
      "oper_tasks": [ "records" ],
      "zone_info": [
        {
          "zone": "example.com",
          "records": $records
        }
      ]
    }
  }
'

它返回:

{
  "extra_vars": {
    "oper_tasks": [
      "records"
    ],
    "zone_info": [
      {
        "zone": "example.com",
        "records": [
          {
            "name": "foo.example.com",
            "type": "CNAME",
            "value": "google.com",
            "ttl": 3600
          },
          {
            "name": "bar.example.com",
            "type": "CNAME",
            "value": "stackoverflow.com",
            "ttl": 3600
          }
        ]
      }
    ]
  }
}

Imagine that you have a file cnames.txt where each line contains one CNAME domain followed by its value:

foo.example.com google.com
bar.example.com stackoverflow.com

Let us create that file:

cat <<EOF > cnames.txt
foo.example.com google.com
bar.example.com stackoverflow.com
EOF

You can "nest" one jq into the other by using --slurpfile described in the jq Manual. The following command should do what you want:

cat cnames.txt | xargs -n2 jq -n '{
    "name": $ARGS.positional[0],
    "type": "CNAME",
    "value": $ARGS.positional[1],
    "ttl": 3600
}' --args | jq -n --slurpfile records /dev/stdin '
  {
    "extra_vars": {
      "oper_tasks": [ "records" ],
      "zone_info": [
        {
          "zone": "example.com",
          "records": $records
        }
      ]
    }
  }
'

which returns:

{
  "extra_vars": {
    "oper_tasks": [
      "records"
    ],
    "zone_info": [
      {
        "zone": "example.com",
        "records": [
          {
            "name": "foo.example.com",
            "type": "CNAME",
            "value": "google.com",
            "ttl": 3600
          },
          {
            "name": "bar.example.com",
            "type": "CNAME",
            "value": "stackoverflow.com",
            "ttl": 3600
          }
        ]
      }
    ]
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文