JSON PATH Field NULL Checking Expression

Yassir Arafat Roni :

I have a json array like bellow :

{
"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,
            "likes": 1
        },
        {
            "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}

I was trying to find all books price which have no likes field. Is it possible to find using Jayway Json Path library. I am trying something like this $.store.book[? (@.likes == null)].price but i don't know whether there is anything like that?

glytching :

This jsonpath expression is correct:

$.store.book[?(@.likes == null)].price

But you must set DEFAULT_PATH_LEAF_TO_NULL, for example:

Configuration conf = Configuration.defaultConfiguration()
    .addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);

String pricesForBooksWithNoLikes = JsonPath.using(conf).parse(json)
    .read("$.store.book[?(@.likes == null)].price");

More details in the docs.

Here's a screenshot showing this read working, using the Jayway JsonPath Evaluator:

enter image description here

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=468795&siteId=1