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

Any chance of adding recursive descent support? #130

Closed
TomAnthony opened this issue Aug 8, 2019 · 4 comments
Closed

Any chance of adding recursive descent support? #130

TomAnthony opened this issue Aug 8, 2019 · 4 comments

Comments

@TomAnthony
Copy link

As detailed at https://goessner.net/articles/JsonPath/ - recursive descent allows specifying elements without needing to give their whole path.

The syntax clashes with the JSON Lines syntax, but it could be implemented differently.

It is very helpful for deeply nested elements where it is hard to get a whole path; as a simple example:

{ "something": {
    "anything": {
      "abcdefg": {
          "finally": {
            "important": {
                "secret": "password"
            }
        }
      }
    }
  }
}

With recursive descent you could do $..secret and get [ "password"] as the output.

Are there plans, or appetite, to support such a feature?

Thanks!

@tidwall
Copy link
Owner

tidwall commented Aug 8, 2019

I have no plans at this time, but I think it's an interesting idea.

Perhaps using three dots followed by the path.

@tidwall
Copy link
Owner

tidwall commented Oct 21, 2019

I'm closing for now, but please reopen if you further ideas.

@tidwall tidwall closed this as completed Oct 21, 2019
@xxxserxxx
Copy link

xxxserxxx commented May 20, 2020

@tidwall closed, because you aren't interested in implementing it, or closed because you would like more input on how it'd be implemented?

XPath, for XML, has wildcard path support with the ancestor() and descendant() (a.k.a. //) axes. It's a pain to do in jq as well, and is (IME) one of the most common operations users would like to do. There's a lot of JSON with deeply nested schemas, and -- unlike XML -- JSON is expected to be, and frequently is, far less restricted in structure.

A real-life use-case is the Linux window manager, i3. i3 has a command-line tool called i3-msg with which users can dump out a JSON representation of the desktop tree. Because i3 supports containers, the tree can be arbitrarily nested, and windows could be 3 levels deep, or 10 levels deep. A common problem I face when writing scripts is how to find the ID of an arbitrary window that has a certain WM NAME attribute. This is an absurdly difficult problem to solve in jq, and without a path wildcard element, it's almost impossible in jj.

Support for some sort of wildcard syntax (*, ..., %, // -- almost any commonly used wildcard) would be a great addition.

tidwall added a commit that referenced this issue Aug 9, 2023
This commit adds the "@dig" modifier, which allows for searching
for values in deep or arbitrarily nested json documents

For example, using the following json:

```
{ "something": {
    "anything": {
      "abcdefg": {
          "finally": {
            "important": {
                "secret": "password"
            }
        }
      }
    }
  }
}
```

```
@dig:secret  ->  ["password"]
```

See #130
@tidwall
Copy link
Owner

tidwall commented Aug 9, 2023

I just added the @dig modifier:

{ "something": {
    "anything": {
      "abcdefg": {
          "finally": {
            "important": {
                "secret": "password"
            }
        }
      }
    }
  }
}
@dig:secret => ["password"]

I hope you find this helpful.

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

3 participants