简体   繁体   中英

Filter values by regex in JsonPath

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,

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM