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

How to assert if a json object is included in a collection #420

Closed
fjchen7 opened this issue Dec 17, 2021 · 8 comments
Closed

How to assert if a json object is included in a collection #420

fjchen7 opened this issue Dec 17, 2021 · 8 comments
Milestone

Comments

@fjchen7
Copy link

fjchen7 commented Dec 17, 2021

Assume that we have the json

{
    "fruit": [
        {
            "name": "apple",
            "price": 100
        },
        {
            "name": "grape",
            "price": 200
        }
    ]
}

How to assert that {"name": "apple", "price": 100} is in collection? For now what I can do is that

jsonpath "$.fruit[*].name" includes "apple"
jsonpath "$.fruit[*].price" includes 100

But this is not the exact assertion I want. It checks these two values separately.

Seems that the keyword includes only supports literal value? Can we extend it to json object? Just like

jsonpath "$.fruit[*]" includes '{"name": "apple", "price": 100}'
@fabricereix
Copy link
Collaborator

fabricereix commented Dec 17, 2021

Hi @fjchen7,

You're right, currently includes (all other predicates) only support scalar literal values. List and Object are not supported.
If these one are parseable without ambiguities, it might indeed be a good idea to support it. We are going to look into it.

Note that you can for the time-being use the following jsonpath asserts in order to be more specific:

jsonpath "$.fruit[?(@.name=='apple')].price" == 100
jsonpath "$.fruit[?(@.name=='grape')].price" == 200

But I agree that yours would be more readable

jsonpath "$.fruit[*]" includes {"name": "apple", "price": 100}
jsonpath "$.fruit[*]" includes {"name": "grape", "price": 200}

@fjchen7
Copy link
Author

fjchen7 commented Dec 17, 2021

Great. Thanks @fabricereix, your answer could meet most of my requirement.

BTW, can I filter nested element?

{
    "fruit": [
        {
            "name": "apple",
            "price": {
                "US": 100,
                "UN": 110
            }
        },
        {
            "name": "grape",
            "price": {
                "US": 200,
                "UN": 150
            }
        }
    ]
}

I tried with this

jsonpath "$.fruit[?(@.name.US==200)].name == "grape"

But it returns is not valid.

@fabricereix
Copy link
Collaborator

I don't think this is a valid jsonpath expression.

You can always quickly check with http://jsonpath.com/.
It also does not return anything.

@fjchen7
Copy link
Author

fjchen7 commented Dec 17, 2021

Great project and we are ready to use it for production test. Thanks for help!

@fjchen7 fjchen7 closed this as completed Dec 17, 2021
@fjchen7 fjchen7 reopened this Dec 18, 2021
@fjchen7
Copy link
Author

fjchen7 commented Dec 18, 2021

Sorry my mistake. I gave a wrong jsonpath expression in my last reply.

Actually the expression $.fruit[?(@.price.US==200)].name can filter out value in http://jsonpath.com.

image

However in hurl the same expression can not work.

image

hurl can not support full jsonpath?

@fabricereix
Copy link
Collaborator

The original "spec" https://goessner.net/articles/JsonPath/ is still the one we refer to to, but it is not very detailed.
Every implementation adds its own features, which are not described in this spec. For example, your filter with a nested object is one of them.

We also add these extra features when they make sense and often they are also implemented in http://jsonpath.com.
This one is indeed a good candidate. We can try to support it for the next release (#423)

@fabricereix
Copy link
Collaborator

@fjchen7, the support for nested object has now been merged into master.
you can get the hurl binary from https://github.com/Orange-OpenSource/hurl/actions/runs/1603719015#artifacts if you need this feature before the next release (probably end of january).
Thanks for your feedback.

@fjchen7
Copy link
Author

fjchen7 commented Dec 21, 2021

Amazing. You guys are very productive. Very appreciate it!

@fjchen7 fjchen7 closed this as completed Dec 21, 2021
@jcamiel jcamiel added this to the 1.6.0 milestone Jan 31, 2022
@fabricereix fabricereix added enhancement New feature or request and removed enhancement New feature or request labels Feb 10, 2022
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