Skip to content

Assert against every member of a JSONPATH collection?Β #347

Open
@danielbprice

Description

@danielbprice

Hi, I'm sorry if this is an ignorant question. Let's imagine an API which returns an array of elements:

{
  "data": [
    { "item": "value1" },
    { "item": "value2" },
    { "item": "value3" }
  ]
}

My basic use case is that I want to assert that all of the values of "item" match the pattern "value.*". Today it seems like I can do:

jsonpath "$.data[0].item" matches "value.*"

However I would really like a way to say:

jsonpath "$.data[*].item" matches "value.*"

That is to say, test each member against the regex. What seems to happen when I try this is that all of the items are concatenated, so that the match is against something like [string <value1>, string <value2>, string <value3>] (according to the output of hurl). I can see using https://jsonpath.com/ that this is due to the way jsonpath processes the input.

I tried to bend jsonpath to emit a collection of elements whose value does not match value.* (so I could assert that zero things remained) but couldn't figure it out.

Something I wondered along the way is whether it would make sense to be able to run the predicate on each value in a resultant collection. So jsonpath "$.data[*].item" EACH matches "value.*"

Thanks for your consideration! I have had good success using hurl to help me build an API gateway.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions