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

2019-08-26 21:11发布

问题:

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
'