I am working with a JSON schema and trying to validate specific field. Normally I specify a path to retrieve a value for validation based on the position of the object in the array as such:
mapList.Object[0].memberAmount
But in the situation below , the order of the objects changes each time , so I need to chose a field from a specific object from the array. How can I write a path with following logic ? give me a memberAmount field value from the object where the typeID is 23098 (this field value is always unique).That way even if the order of the objects changes in the array, I will still have the correct field value from the correct object.
"MapList": {
"Object": [
{
"TypeId": 230098,
"mIndicator "P",
"memberAmount": 110.00,
"edAmount": 0.00,
"plra": 0.00,
"spaf": 0.00,
"grossAmount": 110.00,
},
{
"TypeId": 230099,
"mIndicator "A",
"memberAmount": 900.00,
"edAmount": 0.00,
"plra": 0.00,
"spaf": 0.00,
"grossAmount": 110.00,
},
{
"TypeId": 230100,
"mIndicator "A",
"memberAmount": 660.00,
"edAmount": 0.00,
"plra": 0.00,
"spaf": 0.00,
"grossAmount": 110.00,
}
]
}
2
Answers
To find the particular element, you can use
JsonPath.parse()
, which returns aList<Map<String, Object>>
, which can be traversed to retrieve your target value:You may consider library Josson to retrieve value with simple query expression.
https://github.com/octomix/josson
Deserialization
Query