1
0
-1

I have an json array data like below

test = '''{   "data": [     {    "CG_OCCUPATION_COD": "A16",    "DESCR": "ATM Machines Operator"  },  {    "CG_OCCUPATION_COD": "A04",    "DESCR": "Accessories Store Empl"  },  {    "CG_OCCUPATION_COD": "A01",    "DESCR": "Accountant"  } ,  {    "CG_OCCUPATION_COD": "A02",    "DESCR": "Accounting Executive"  },  {    "CG_OCCUPATION_COD": "A03",    "DESCR": "Accounting Office Empl"  },  {    "CG_OCCUPATION_COD": "A05",    "DESCR": "Actor"  },  {    "CG_OCCUPATION_COD": "A06",    "DESCR": "Actuary"  }  ] } '''

 

how can I get a sub array based on certain  "DESCR" values? let say "DESCR" value containing 'account' string

I will use Path class from TM

Path jsonPath = new Path(test )

 

    CommentAdd your comment...

    1 answer

    1.  
      1
      0
      -1

      The following will give you an array of Paths that you can interrogate:

      jsonPath.paths("data[*]").findAll{ it.val("DESCR").toLowerCase().indexOf("account") >= 0 }.each {
          println it.val("CG_OCCUPATION_COD") + ' - ' + it.val("DESCR")
      }

      Or, if you just wanted an array of matching DESCR string values for example:

      jsonPath.vals("data[*].DESCR").findAll{ it.toLowerCase().indexOf("account") >= 0 }.each {
          println it
      }