I want to convert string into Map so I can get key and values from it for example:

String fields = '[{"Title":"Name"},{"Product_type":"IsActive"}]';

convert into map and get the value

system('Value '+ map.get(Title));

system('Value '+ map.get(Product_type));

How to do this?


Just to add on @sfdcfox answer, you can still use JSON.deserializeUntyped but first you will need to convert to List<Object> to prevent the error Invalid conversion from runtime type List<ANY> to Map<String, ANY>

After converting to List, you can build the map as follows:

String fields = '[{"Title":"Name"},{"Product_type":"IsActive"}]';
Map<String, Object> valuesMap = new Map<String, Object>();

for(Object obj : (List<Object>)JSON.deserializeUntyped(fields)){
   //convert values to map
   Map<String, Object> data = (Map<String, Object>)obj;
   valuesMap.putAll(data );

System.debug(valuesMap.get('Title')); // 'Name';
System.debug(valuesMap.get('Product_type')); // 'IsActive';
  • Excellent- this worked for me- thanks!
    – Navigatrix
    Aug 11 '21 at 20:54

You can do this "for free" with JSON.deserializeUntyped:

Map<String, Object> values = (Map<String, Object>)JSON.deserializeUntyped(fields);
System.debug(values.get('Title')); // 'Name';
System.debug(values.get('Product_type')); // 'IsActive';

Other approaches are possible. For example, if you know all values will be strings:

Map<String, String> values = (Map<String, String>)JSON.deserialize(fields, Map<String, String>.class);
System.debug(values.get('Title')); // 'Name';
System.debug(values.get('Product_type')); // 'IsActive';

You can use almost any kind of primitive in complicated ways if you know the final results will parse out that way.

  • It getting me runtime error: Invalid conversion from runtime type List<ANY> to Map<String, ANY> Dec 18 '19 at 7:18
  • @ManishJodhani i've posted a variant of this answer to help you fix the runtime error
    – Shamina
    Dec 18 '19 at 8:39

This works:

//Apex wrapper class
public class ConvertList {
    public TitleWrapper titleIns;
    public ProductWrapper productIns;


public class TitleWrapper {
    String Title; //Name


public class ProductWrapper {
    String Product_Type; //IsActive

String fields = '[{"Title":"Name"},{"Product_type":"IsActive"}]';

List<ConvertList> convertListObj = (List<ConvertList>)System.JSON.deserialize(fields, List<ConvertList>.class);
  • It's getting this error: System.JSONException: Malformed JSON: Expected '{' at the beginning of object Dec 18 '19 at 7:30
  • 1
    This answer would be improved if you went into more detail about how it works. Otherwise, it's just a code dump, and other people with similar questions won't be able to figure out what changes they need to make to get this to work for them.
    – Derek F
    Dec 18 '19 at 13:35

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.