Is it possible with jq to use a deleted value in s

2019-08-26 20:49发布

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条回答
乱世女痞
2楼-- · 2019-08-26 20:58

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
'
查看更多
登录 后发表回答