Reputation: 202
I am new to JSON. I have a LossHistory Json from which I need to filter "LineOfBusinessCode" equal to "General Liab - Excess" from that I need to show specific "Lossyear" My JSON data
{"LossHistory": {
"LossDetails": [
{
"LineOfBusinessCode": "General Liab - Excess",
"Lossyear": 2018,
"NumberOfTotalClaims": 8,
"PaidLosses": 500,
"PaidExpense": 80000,
"Outstandingreserve": 800,
"TotalIncurred": 800,
"valuationDate": "2023-12-06T00:00:00",
"LargeClaim": 700,
"NumberOfOpenClaims": 6,
"carrierName": "Hrst",
"ClaimSource": "Manual"
},
{
"LineOfBusinessCode": "General Liab - Excess",
"Lossyear": 2019,
"NumberOfTotalClaims": 8,
"PaidLosses": 500,
"PaidExpense": 80000,
"Outstandingreserve": 800,
"TotalIncurred": 800,
"valuationDate": "2023-12-06T00:00:00",
"LargeClaim": 700,
"NumberOfOpenClaims": 6,
"carrierName": "Hrst",
"ClaimSource": "Manual"
},
{
"LineOfBusinessCode": "Auto",
"Lossyear": 2018,
"NumberOfTotalClaims": 8,
"PaidLosses": 500,
"PaidExpense": 80000,
"Outstandingreserve": 800,
"TotalIncurred": 800,
"valuationDate": "2023-12-06T00:00:00",
"LargeClaim": 700,
"NumberOfOpenClaims": 6,
"carrierName": "Hrst",
"ClaimSource": "Manual"
}
]
}}
my path
$..LossDetails[?(@.LineOfBusinessCode == 'General Liab - Excess')].Lossyear
now my result is
[
2018,
2019
]
I'm getting as expected now I need 2018 or 2019 any one of them. We can use index or anyother way but we need to go with this condition LineOfBusinessCode is equal to "General Liab - Excess" then lossyear expected output
2018
Upvotes: 0
Views: 48
Reputation: 2518
According to this issue (https://github.com/json-path/JsonPath/issues/272), it is impossible just use JsonPath
to achieve it in one line syntax. But you can just get the result and then get(0)
to get it easily ,such as:
String filterResult = JsonPath.read(fullJson, "$..LossDetails[?(@.LineOfBusinessCode == 'General Liab - Excess')]").toJSONString();
int lossYear = JsonPath.read(filterResult, "$[0].Lossyear");
Upvotes: 1