0

I was trying to find a way to filter values by regex using JsonPath from Jayway and so far I could more or less do what I want to do.

I'm trying to filter results by values which does not start with 'foo'. To my regex knowledge it should be done by '$..book[?(@.author =~ /^[^foo]/)].author' with the given JSON of

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

but it doesn't return any values although none of the values are not starting with 'foo'. I've tried it via using the online tool: https://jsonpath.herokuapp.com/

In addition, I can't solve this problem with any Java code or Filters that would be written in Java or similar. Regex would be my more or less only choice here.

Anybody knows how to solve this problem?

Cheers,

4
  • 1
    This: $..book[?(@.author =~ /.*REES/i)] returns the book object so this: $..book[?(@.author =~ /.*REES/i)].author returns the author(s). That answer seem too simple so I'm probably missing something. If so, perhaps you could update your question to include a sample JSON document and your desired output? Commented May 8, 2018 at 14:15
  • @glytching Thanks for the reply, that solves my problem partially however, it still doesn't work with startsWith expression. I've tried via this tool -> jsonpath.herokuapp.com using the provided JSON, normally this expression should be working and returning results '$..book[?(@.author =~ /^[^_]/)].author', i'm basically trying to return all the results which does not start with '_'. Commented May 8, 2018 at 14:39
  • Can you be specific about what this means: "it still doesn't work with startsWith expression"? Is JsonPath throwing an exception? or just not returning the correct results? If the former could you update the question to include details of the exception? If the latter, could you update the question to describe exactly what JsonPath is returning? Commented May 8, 2018 at 14:49
  • Basically it returns empty list of results, I'll update the question. Thanks! Commented May 8, 2018 at 14:50

1 Answer 1

1

to filter results by values which does not start with 'foo', try: $..book[?(@.author =~ /^(?!foo).*/i)]

Regex: /^(?!foo).*/i) returns a match only if it doesn't start with foo (using negative lookahead)

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

Comments

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.