0

I have this json string.

[
  {
    "id":2,
    "name":"Abc_SS",
    "description":"Abc"
  },
  {
    "id":100,
    "name":"sales",
    "description":"sales"
  }
]

How I can convert this json to List<Map<String, Object>>. Is it possible?

  • Nope, the JSON string contains a list of Objects. – rahul gawale Jan 28 at 6:30
  • Basically, apparently with Apex JSON.deserialize needs a concrete type and doesn't allow object :/ So you need to define a type – zaitsman Jan 28 at 7:07
5

It is impossible explicitly, however, you can deserialize you String into List< Object> and then cast your Object in for-loop to Map< String, Object>:

String jsonString = '[{"id":2,"name":"Abc_SS","description":"Abc"},{"id":100,"name":"sales","description":"sales"}]';

List<Object> items = (List<Object>) JSON.deserializeUntyped(jsonString);

for (Object itemObj : items) {
    Map<String, Object> item = (Map<String, Object>) itemObj;
    //now you can work with it like you always work with Map
}
|improve this answer|||||
1

My overlapping answer that converts to the list of map of string to object is very like Mariia's:

String theJsonString = '[{"id":2, "name":"Abc_SS", "description":"Abc"}, {"id":100, "name":"sales", "description":"sales"}]';
Object theJsonObject = JSON.deserializeUntyped(theJsonString);
List<Object> theJsonList = (List<Object>) theJsonObject;
List<Map<String, Object>> theJsonMapList = new List<Map<String, Object>>();

for (Object obj : theJsonList) {
    theJsonMapList.add((Map<String, Object>) obj);
}

System.debug(theJsonMapList);

As she said, you have to do it in steps as there's no way to just do a cast.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.