Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recurse(), but preserve structure #263

Closed
1ace opened this issue Jan 4, 2014 · 7 comments
Closed

recurse(), but preserve structure #263

1ace opened this issue Jan 4, 2014 · 7 comments

Comments

@1ace
Copy link

1ace commented Jan 4, 2014

Let's take the recurse example from the manual, and add a bunch of useless {"foo": "bar"} to it:

{"name": "/", "foo": "bar", "children": [
  {"name": "/bin", "foo": "bar", "children": [
    {"name": "/bin/ls", "foo": "bar", "children": []},
    {"name": "/bin/sh", "foo": "bar", "children": []}]},
  {"name": "/home", "foo": "bar", "children": [
    {"name": "/home/stephen", "foo": "bar", "children": [
      {"name": "/home/stephen/jq", "foo": "bar", "children": []}]}]}]}

recurse(.children[]) | .name will give me all the names, but destroy the structure of the JSON in the process.

Is there a way to get that information, but preserve the structure?

With the JSON above, this would be the output:

{"name": "/", "children": [
  {"name": "/bin", "children": [
    {"name": "/bin/ls", "children": []},
    {"name": "/bin/sh", "children": []}]},
  {"name": "/home", "children": [
    {"name": "/home/stephen", "children": [
      {"name": "/home/stephen/jq", "children": []}]}]}]}
@nicowilliams
Copy link
Contributor

@1ace You want to remove keys that are not of interest, is that it?

@ghost
Copy link

ghost commented Nov 28, 2014

I was lurking through the query label and found this issue. Better late than never, I guess!

In order to remove the "foo" attribute from each element of the structure, you want to recurse through the structure and set each element to the result of deleting the foo attribute from itself. This translates to jq as:

recurse(.children[]) |= del(.foo)

If, instead of blacklisting foo, you'd rather whitelist name and children, you could do something like:

recurse(.children[]) |= {name, children}

@nicowilliams
Copy link
Contributor

@slapresta That's a neat idiom we should add to the wiki. Thanks!

@ghost
Copy link

ghost commented Dec 24, 2014

I think we can close this, but I don't have permission to do so.

@nicowilliams
Copy link
Contributor

I left it open because I haven't checked whether the wiki mentions this, and it's worth making sure it does.

@pkoppstein
Copy link
Contributor

The idioms are described at
https://github.com/stedolan/jq/wiki/jq-Cookbook#delete-elements-from-objects-recursively

@1ace 1ace closed this as completed Dec 26, 2014
@1ace
Copy link
Author

1ace commented Dec 26, 2014

Thanks :)

@dtolnay dtolnay added support and removed query labels Jul 25, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants