Search code examples
javajsonrest-assuredjsonpathrest-assured-jsonpath

Extract data from JSONArray using jsonpath


I have a JSON file having customer data as below . Here I want to extract the custName & CustId where amount is greater than 10 . But I'm stuck as unable to get the desired jsonpath for extracting these values .

  "customers": [
    {
      "custId": 540,
      "custName": "John",
      "itemId": 647,
      "itemAmount": 3000
    },
    {
      "custId": 432,
      "custName": "Adrian",
      "itemId": 600,
      "itemAmount": 2000
    },
    {
      "custId": 541,
      "custName": "Smith",
      "itemId": 320,
      "itemAmount": 1200
    }
  ]
} 

I tried something like - $.customers[*].itemAmount which gave me a list of itemAmount but my objective is entirely different so looking for the jsonPath expression to get the desired data.


Solution

  • To get both fields for everything meeting the criterion:

    $.customers[?(@.itemAmount > 10)]['custName','custId']
    

    I recommend adding some records that do not meet the criterion to your sample so you can be sure it is working.