0

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?

3 Answers 3

3

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';
1
  • Excellent- this worked for me- thanks!
    – Navigatrix
    Commented Aug 11, 2021 at 20:54
2

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.

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

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);
2
  • It's getting this error: System.JSONException: Malformed JSON: Expected '{' at the beginning of object Commented Dec 18, 2019 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
    Commented Dec 18, 2019 at 13:35

You must log in to answer this question.

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