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

Filter by nested arrays #2263

Open
aageneralov opened this issue Dec 19, 2023 · 5 comments
Open

Filter by nested arrays #2263

aageneralov opened this issue Dec 19, 2023 · 5 comments
Labels
enhancement New feature or request topic: jsonpath

Comments

@aageneralov
Copy link

aageneralov commented Dec 19, 2023

Problem to solve

I have some trouble with filter elements in json array by nested arrays elements
Data:

{
  "docs": [
    {
      "title": "foo",
      "authors": [
        "one",
        "two"
      ]
    },
    {
      "title": "bar",
      "authors": [
        "one",
        "three"
      ]
    },
    {
      "title": "baz",
      "authors": [
        "two",
        "three"
      ]
    }
  ]
}  

I need to assert that data have doc with author named "two", I tried to use many variants of jsonpath:

$.docs[?(@.authors.indexOf('two') != -1)]
$.docs[?(@.authors[*] == 'two')]
$.docs[?(@.authors[?(@ == 'two')])]

But it doesn't work

Proposal

Please add an indexOf function or something like that

@aageneralov aageneralov added the enhancement New feature or request label Dec 19, 2023
@fabricereix
Copy link
Collaborator

Hi @aageneralov,
I agree that we need to find a way of doing it.
We need to look into it. It could be interesting to see how jsonpath.com supports that.
The spec https://www.ietf.org/archive/id/draft-goessner-dispatch-jsonpath-00.html has not defined yet such functions (such as indexOf).

We also plan to add support for combining multiple filter expressions (#1697)
In this case, we could write something like $.docs[?(@.authors[1]=="two" || @.authors[0]=="two")].

@dorryspears
Copy link
Contributor

@fabricereix what do you think about adding a "for all" clause on the on the assert

So for this example you would write

jsonpath $..authors contains "two" for all

@fabricereix
Copy link
Collaborator

yes we plan to add predicates that are applied to each element of a list.
you can have a look at #347

@dorryspears
Copy link
Contributor

@fabricereix I will try to tackle the feature

@CrispyDrone
Copy link

What do you think about integrating hurl with any arbitrary tools to do processing? So, you could just invoke a jq or yq command during filters, assertions,...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request topic: jsonpath
Projects
None yet
Development

No branches or pull requests

5 participants