Is there a simple way to extract a sub tree from JSON, keeping the path?
I am using
cat composer.json | jq .repositories > /tmp/composer.json
But this would miss the repositories
node it self.
How can I filter a composer.json to include only certain sub trees?
CodePudding user response:
Given an object, .repositories
fetches the content of a field called repositories
. To wrap that content inside another object under a field named again repositories
, create that object with such field and add the content using {repositories: .repositories}
. As this pattern is so common, there is a shorthand for it: {repositories}
.
jq '{repositories}' composer.json
To "extract" more than just one field in the same object, separate the field names with commas: {x,y,z}
. To "extract" more than one field, each in its own object, separate the objects with commas: {x},{y},{z}
.