Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check JSONPath for empty string

Tags:

jsonpath

I have the following JSON string:

{
  "myKey": "myValue"
}

I want to check whether this string is not empty. I can do this:

$.myKey

to get the value of myKey. I've tried changing it to

$.[?(@.myKey.length()>0)]

(as per this topic Expression to filter out elements with empty arrays using `jsonPath`) or even this:

$.myKey.length()>0

but I get error that it is not a valid JSONPath expression.

I add "JsonPath Match" to the test step in my project. When I set it to

$.myKey

and press "Select from current", it works. For

$.[?(@.myKey != '')]

when I press "Select from current", I get

"Invalid JsonPath expression"

I use SoapUI 5.4.0 but I don't know how to check its JsonPath implementation. I want to get false if string is empty.

How can I check whether myValue is empty or not? Thanks!

like image 803
user1809566 Avatar asked Oct 21 '25 19:10

user1809566


1 Answers

The following jsonpath filter: $.[?(@.myKey != '')] will return the document if the attribute myKey is not an empty string.

You can verify this using the Jayway JsonPath evaluator.

If this ...

How can I check whether myValue is empty or not?

... means something like: "I only want to return the document if myKey is populated" then the above jsonpath filter should suffice.

like image 144
glytching Avatar answered Oct 27 '25 02:10

glytching



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!