Is it possible with jq to use a deleted value in setting new ones?

三世轮回 提交于 2020-01-25 08:29:04

问题


I am using the bash to json parser jq

Considering the following command:

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

And the following input:

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

I am trying to get the following output:

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

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


回答1:


Transforming your input to your output is as simple as:

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

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

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

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
'


来源:https://stackoverflow.com/questions/50937290/is-it-possible-with-jq-to-use-a-deleted-value-in-setting-new-ones

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!