使用jq从变量更改json对象和字段值 [英] Using jq to change json object and field value from variable

查看:331
本文介绍了使用jq从变量更改json对象和字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用在Linux bash shell中使用jq指定的变量来修改以下json.

I want to modify the following json using variables specified in a Linux bash shell using jq.

var1="red"
var2="european.flowers"
var3="european_vegetables"
var4="20"

我的json:

{
 "plants": {
  "flowers.small": {
    "colour": "",
    "age": "",
    "vegetables": {
     "root": "",
     "height": ""
    }
  }
 }
}

我想使用jq中的变量修改json:

I want to modify the json using variables in jq:

{
 "plants": {
  "${var2}": {
    "colour": "${var1}",
    "age": "",
    "${var3}": {
     "root": "",
     "height": "${var4}"
    }
  }
 }
}

我正在尝试仅通过变量设置字段值:

I am attempting to just set a field value from a variables:

命令:

cat myjson.json|jq '.plants["flowers.small"].colour = "${var1}"' -c

结果是:

{"plants":{"flowers.small":{"colour":"${var1}","age":"","vegetables":{"root":"","height":""}}}}

命令:

cat myjson.json|jq --arg v "$var1" '.plants.["flowers.small"].colour = [$v]' -c

结果是:

jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.plants.["flowers.small"].colour = $v
jq: 1 compile error

我的jq版本是:jq-1.5-1-a5b5cbe

My jq version is: jq-1.5-1-a5b5cbe

如何重命名字段并从变量中设置键的值? 使用jq版本甚至可以做到吗?

How can I rename a field and set a value for the key from the variables? Is this even doable using the jq version?

推荐答案

使用jq作为模板引擎

如果您真的不需要 input.json作为单独的文件,则很容易做到的是将整个模板定义为jq表达式:

Using jq as a template engine

If you don't really need input.json to be a separate file, the easy thing to do is to define your entire template as a jq expression:

var1="red"
var2="european.flowers"
var3="european_vegetables"
var4="20"
jq -n --arg var1 "$var1" --arg var2 "$var2" --arg var3 "$var3" --arg var4 "$var4" '
{
 "plants": {
  "\($var2)": {
    "colour": $var1,
    "age": "",
    "\($var3)": {
     "root": "",
     "height": $var4
    }
  }
 }
}'

作为输出发出:

{
  "plants": {
    "european.flowers": {
      "colour": "red",
      "age": "",
      "european_vegetables": {
        "root": "",
        "height": "20"
      }
    }
  }
}


或者:重命名子树

如果您确实要重命名现有密钥,请考虑以下方法.在模板字段中使用jq变量不是强制性的,但这确实使以后更改代码更容易:


Alternately: Renaming subtrees

If you really do want to rename an existing key, consider an approach like the below. Using jq variables for the template fields isn't mandatory, but it does make it easier to change your code later:

var1="red"; var2="european.flowers"; var3="european_vegetables"; var4="20"

jq --arg var1 "$var1" --arg var2 "$var2" --arg var3 "$var3" --arg var4 "$var4" '

  # set variables for the fields we expect to see in our input
  "flowers.small" as $plant_tmpl |
  "vegetables" as $cat_tmpl |

  # change things inside fields we will later rename *before* we rename those fields
  .plants[$plant_tmpl].colour = $var1 |
  .plants[$plant_tmpl][$cat_tmpl].height = $var4 |

  if $var3 == $cat_tmpl then . else
    # var3 is not "vegetables" (templated value), so we need to rename it
    .plants[$plant_tmpl][$var3] = .plants[$plant_tmpl][$cat_tmpl] |
    del(.plants[$plant_tmpl][$cat_tmpl])
  end |

  if $var2 == $plant_tmpl then . else
    .plants[$var2] = .plants[$plant_tmpl] |
    del(.plants[$plant_tmpl])
  end
' <<'EOF'
{
 "plants": {
  "flowers.small": {
    "colour": "",
    "age": "",
    "vegetables": {
     "root": "",
     "height": ""
    }
  }
 }
}
EOF

这篇关于使用jq从变量更改json对象和字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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