skip to Main Content

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


  1. To find the particular element, you can use JsonPath.parse(), which returns a List<Map<String, Object>>, which can be traversed to retrieve your target value:

    String memberAmount = (String)JsonPath.parse(jsonString)
      .read("$['Object'][?(@['TypeId'] == 230098)]")
      .get(0)
      .get("memberAmount");
    
    Login or Signup to reply.
  2. You may consider library Josson to retrieve value with simple query expression.

    https://github.com/octomix/josson

    Deserialization

    Josson josson = Josson.fromJsonString(
        "{" +
        "    "MapList": {" +
        "        "Object": [" +
        "            {" +
        "                "TypeId": 230098," +
        "                "mIndicator": "P"," +
        "                "memberAmount": 110.0," +
        "                "edAmount": 0.0," +
        "                "plra": 0.0," +
        "                "spaf": 0.0," +
        "                "grossAmount": 110.0" +
        "            }," +
        "            {" +
        "                "TypeId": 230099," +
        "                "mIndicator": "A"," +
        "                "memberAmount": 900.0," +
        "                "edAmount": 0.0," +
        "                "plra": 0.0," +
        "                "spaf": 0.0," +
        "                "grossAmount": 110.0" +
        "            }," +
        "            {" +
        "                "TypeId": 230100," +
        "                "mIndicator": "A"," +
        "                "memberAmount": 660.0," +
        "                "edAmount": 0.0," +
        "                "plra": 0.0," +
        "                "spaf": 0.0," +
        "                "grossAmount": 110.0" +
        "            }" +
        "        ]" +
        "    }" +
        "}");
    

    Query

    Double memberAmount = josson.getDouble("MapList.Object[TypeId=230098].memberAmount");
    System.out.println(memberAmount);
    // Output: 110.0
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search