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

Select path that is not an object #2303

Closed
bugb opened this issue Apr 19, 2021 · 2 comments
Closed

Select path that is not an object #2303

bugb opened this issue Apr 19, 2021 · 2 comments

Comments

@bugb
Copy link

bugb commented Apr 19, 2021

I have an object save in file name e like this

{
  "a": {
    "b": 1,
    "c": 2
  }
}

Now I can get all the path with:
cat e | jq '[paths]' as myjson

Output:

[
  [
    "a"
  ],
  [
    "a",
    "b"
  ],
  [
    "a",
    "c"
  ]
]

It works, but I do not want to have the path a because jq '.a' is an object. I only want

[
  [
    "a",
    "b"
  ],
  [
    "a",
    "c"
  ]
]

How can I do it?!

@itchyny
Copy link
Contributor

itchyny commented Apr 19, 2021

This is what leaf_paths (alias of paths(scalars)) is defined for; jq '[leaf_paths]', but as it is broken on falsy values (#1163, #2288), so in the meantime you can use jq '[paths([scalars] != [])]', or if you may allow to include the array paths, use jq '[paths(type != "object")]'.

Also, for future reference, it's usually better to ask usage questions at stackoverflow.com, using the jq tag:
https://stackoverflow.com/questions/tagged/jq

@bugb
Copy link
Author

bugb commented Apr 19, 2021

@itchyny thank you a lot. Closed!

@bugb bugb closed this as completed Apr 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants