Home > database >  Extract data from JSONArray using jsonpath
Extract data from JSONArray using jsonpath

Time:12-29

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.

CodePudding user response:

Try this:

custName when amount > 10 - $.customers[?(@.itemAmount > 10)].custName

custId when amount > 10 - $.customers[?(@.itemAmount > 10)].custId

CodePudding user response:

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.

  • Related