用bash中的数组替换键 [英] replace a key with array in bash

查看:50
本文介绍了用bash中的数组替换键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例json文件payload.json.tpl:

{
  "foo": "bar",
  "x": {
    "y": "${array}"
  }
}

我在bash中有一个数组

I have an array in bash

array=("one" "two" "three")

如何运行jq命令将键.x.y替换为["one", "two", "three"]

How can I run the jq command to replace the key .x.y to ["one", "two", "three"]

因此最终的json将是:

So the final json will be:

{
  "foo": "bar",
  "x": {
    "y": ["one", "two", "three"]
  }
}

推荐答案

使用$ARGS.positional(需要jq 1.6)

Using $ARGS.positional (requires jq 1.6)

$ array=("one" "two" "three")
$ jq '.x.y = $ARGS.positional' payload.json.tpl --args "${array[@]}"
{
  "foo": "bar",
  "x": {
    "y": [
      "one",
      "two",
      "three"
    ]
  }
}

这篇关于用bash中的数组替换键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆