4

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!

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Unfortunately, even if it worked in JSONPath evaluator, it didn't work in SoapUI where I wanted to have it
SoapUI .. that's valuable additional information. There are numerous JsonPath implementations and numerous ways of using JsonPath. Perhaps you could update your question to describe (1) exactly how you are using JsonPath; (2) what JsonPath implementaiotn you are using, including its version; (3) how you are applying the JsonPath expression and (4) what result you want to get (e.g. do you want to get the JSON document after applying a given filter? or do you want to get a boolean answer to say whether a given filter is applicable?)
OK, I've added further information to the original post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.