Search code examples
jsonpath

how to parse using Jsonpath?


I want to know the value of 'pageName' where the value of 'createdTs' is '333' in JSON like below, how should I use Jsonpath?

[
   {
      "Test1":{
         "pageName":"Apple"
        },
         "Test1-1":{
            "createdTs":"333"
        }
    },
   {
      "Test2":{
         "pageName":"Tomato"
        },
         "Test2-1":{
            "createdTs":"555"
        }
    }
]

.Test1-1[?(@.createdTs == 333)] would look like this:

[
   {
      "createdTs" : "333"
   }
]

The parsing result I want is this.

[
   {
      "Test1":{
         "pageName":"Apple"
        },
         "Test1-1":{
            "createdTs":"333"
        }
    }
]

Solution

  • if you are using Jayway JSONPath a Java DSL for reading JSON documents, then you can use the these jsonpath expressions.

    $[?(@.['Test1-1'].createdTs == 333)]
    

    OR using in filter operator

    $[?(333 in @..createdTs)]
    

    OR using contains filter operator

    $[?(@..createdTs contains 333)]