Search code examples
jsonpath

DEFAULT_PATH_LEAF_TO_NULL Tweaking Configuration in Jayway JsonPath doesnot return null for inner arrays


I have a json string like:

{
"store": {
    "book": [
        {
            "category": "reference",
            "authorextra": [
              {
                "auth1":"Nigel Rees",
                "auth2":"xxxx"
              }
              ],
            "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
}

I have used the DEFAULT_PATH_LEAF_TO_NULL option while parsing the json.

ReadContext ctx=JsonPath.using(Configuration.builder().options(Option.DEFAULT_PATH_LEAF_TO_NULL).build()).parse(input);

When i read this path :

returnObj = ctx.read($.store.book[*].authorextra[*].auth1);

I get the result as : [ "Nigel Rees" ]

I am expecting : [ "Nigel Rees", null, null, null ]

What configuration am i missing here? Any help is appreciated. TIA !


Solution

  • I think you have overlooked the LEAF part of the DEFAULT_PATH_LEAF_TO_NULL setting. Your path has store --> book --> authorextra --> auth1. So the leafs that will be set to NULL are those auth1 missing for authorextra. As the first book is the only one having authorextra, it is the only one giving you result, as it is the only one where you can look for a leaf. If you try this other json document:

    {
       "store" : {
          "book" : [
             {
                "category" : "reference",
                "authorextra": [
                  {
                    "auth1":"Nigel Rees",
                    "auth2":"xxxx"
                  }
                  ],
                "title" : "Sayings of the Century",
                "price" : 8.95
             },
             {
                "category" : "fiction",
                "authorextra": [
                  {
                    "auth2":"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
    }
    

    You would get ["Nigel Rees", null], because the second book also has authorextra, but has missing auth1, that is, the leaf.