0

I'm trying to compare Name and get same value from JSON in Map<String,Object>.

"data":{
      "list":[
         {
            "Status":"ACTIVE",
            "Name":"AA",
            
         },
         {
            "Status":"CLOSE",
            "Name":"BB",
            
         },
         {
            "Status":"CLOSE",
            "Name":"CC",
            
         },
         
      ]
   }
for(Object obj : (List<Object>) mapCustList.get('list')){
      Map<String, Object> keyMap = (Map<String, Object>)obj;
            if(keyMap.values() == 'AA'){
                mapList.add(keyMap);
            }
        }

the expected result should be {"Status":"ACTIVE","Name":"AA"}

2
  • Hi and welcome to SFSE. The initial JSON snippet you have provided looks incomplete; it is missing the outer object "{" and "}" delimiters. Is that just in this question or is it like that in your actual code? AFAIK this won't parse successfully with JSON.deserializeUntyped.
    – Phil W
    Commented Oct 20, 2020 at 10:20
  • Hi. Thanks. It is just for the question, Sorry. JSON will be complete in actual code.
    – Ssp1145
    Commented Oct 20, 2020 at 10:26

1 Answer 1

1

You are comparing an array with a string. Try updating the code like:

for (Object namedStatusAsObject : (List<Object>) mapCustList.get('list')) {
    Map<String, Object> namedStatus = (Map<String, Object>) namedStatusAsObject;

    if (((String) namedStatus.get('Name')) == 'AA') {
        mapList.add(namedStatus);
    }
}
1
  • The solution is working. Thank you.
    – Ssp1145
    Commented Oct 20, 2020 at 13:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .