jq是否可以在设置新值时使用已删除的值? [英] Is it possible with jq to use a deleted value in setting new ones?

查看:57
本文介绍了jq是否可以在设置新值时使用已删除的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用bash进行json解析器 jq

I am using the bash to json parser jq

考虑以下命令:

jq '. * .transitive | del(.transitive) | del(.scope,.scopedName)' package.json > package.github.json$$

以及以下输入:

{
  "name": "navigation",
  "transitive": {
    "name": "navigation",
    "scope": "bs",
    "scopedName": "@bs/navigation"
  }
}

我正在尝试获得以下输出:

{
  "name": "@bs/navigation"
}

在删除.scopedName之前是否有办法使用它的值来设置.name?

Is there a way before doing the delete of .scopedName, to use it's value to set .name?

推荐答案

将输入转换为输出很简单:

Transforming your input to your output is as simple as:

jq '{"name": .transitive.scopedName}'

...当然,您可以重新排序以设置name 之前删除transitive:

...and of course you could just reorder things to set name before deleting transitive:

jq '.name=.transitive.scopedName | del(.transitive)'

也就是说,如果您真的要先使用del(),则可以将内容保存在变量中,以后再使用:

That said, if you really want to use del() first, you can save content in a variable and use it later:

jq '
  .transitive as $transitive |
  del(.transitive) |
  .name=$transitive.scopedName
'

这篇关于jq是否可以在设置新值时使用已删除的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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