2

I have this JSON:

{
  "Details":{
       "Customer Input": "Type Here",
       "Input Status": 0,
       "CumulativeValues":[
            {
                "LinkToPost" : "www.google.com"
            },
            {
                "ScoreMin" : "0"
            },
            {
                "ScoreMax" : "100"
            }
       ]
  }
}

I have deserialized to get "Details" like below:

  Map<String, Object> detailsMap = (Map<String, Object>)JSON.deserializeUntyped(resp.getBody());
  Map<String, Object> detailDataMap = (Map<String, Object>)detailsMap.get('Details');
  List<Object> cumulativeValuesList = (List<Object>) detailDataMap.get('CumulativeValues');

Now, I am trying to fetch the "ScoreMin" and "ScoreMax" values like below:

  Map<String, Object> getScoreMin = (Map<String, Object>) cumulativeValuesList[1];
  Map<String, Object> getMaxScore = (Map<String, Object>) cumulativeValuesList[2];

Although it works to fetch the ScoreMin and ScoreMax values here, I would like to do it without fetching on basis of Indexes like [1], [2].

I tried with Iterating cumulativeValuesList checking if it Contains "ScoreMin" and "ScoreMax", but the results were Null.

for(Object obj : cumulativeValuesList){
   if(cumulativeValuesList.contains('ScoreMin')){
     Map<String,Object> minimumScoreMap = (Map<String, Object>) obj;
  }
if(cumulativeValuesList.contains('ScoreMax')){
     Map<String,Object> maximumScoreMap = (Map<String, Object>) obj;
  }
}

Kindly suggest me how to get the values of ScoreMin and ScoreMax please.

Thank you.

1 Answer 1

1

Since you have a list of objects, you need to read each object individually. Here's a short demo I wrote up based on your JSON:

String data = '{"Details":{"Customer Input": "Type Here","Input Status": 0,"CumulativeValues":[{"LinkToPost": "www.google.com"},{"ScoreMin": "0"},{"ScoreMax": "100"}]}}';
// Root object
Map<String, Object> root = (Map<String, Object>)JSON.deserializeUntyped(data);
// Details object
Map<String, Object> details = (Map<String, Object>)root.get('Details');
// List of Map objects
List<Object> cumulativeValues = (List<Object>)details.get('CumulativeValues');
Map<String, Object> cumulativeValueMap = new Map<String, Object>();
for(Object cumulativeValue: cumulativeValues) {
    // Cast each object to Map<String, Object>
    Map<String, Object> value = (Map<String, Object>)cumulativeValue;
    // Combine keys into a single Map
    for(String key: value.keySet()) {
        cumulativeValueMap.put(key, value.get(key));
    }
}
System.debug(cumulativeValueMap);

Which outputs:

{LinkToPost=www.google.com, ScoreMax=100, ScoreMin=0}

Your mistake was misunderstanding how List.contains operates. It looks for an exact matching object/value in the list. Since our list objects are actually Map<String, Object> and not a String, this method would always return false.

1
  • Thank you very much. This was very helpful to understand for me.
    – Ravi Kumar
    Nov 21, 2022 at 14:53

You must log in to answer this question.

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