2

Here is my request json.

{
  "apply_header":"  CER202208170008", 
  "sn":"CERL202208170008", 
  "cert_cat":"MCS", 
  "cert_name":"MCS", 
  "prod_attr":"test", 
  "prod_name":"DEG9.28", 
  "power":390405, 
  "component_size":"1170x1096x30", 
  "cert_no":"N8AUK 070321 0135", 
  "effective_start_date":"24/4/2022", 
  "ffective_end_date":"24/4/2023", 
  "status":"success"
}

The error is

System.TypeException: Invalid conversion from runtime type Map<String,ANY> to List Class.TS_Phase2_ProductCertificationToSFDC.doPost: line 13, column 1

Here is my error code. i want to deserialize json the insert the data to salesforce. The first line is the error line

list<Object> dataList = (list<Object>)JSON.deserializeUntyped(jsonStr);
for(Object objData : dataList){
    Map<String, Object> dataObjectMap = (Map<String, Object>)objData;
    if(dataObjectMap != null && dataObjectMap.size() > 0){
        TS_Product_Certification__c oneProductC = new TS_Product_Certification__c();
        for(Interface_Field_Mapping__c ifm:fieldList){
            if(dataObjectMap.get(ifm.TS_Interface_Field__c) != null){
                try {}catch{}
            }
        }
2
  • You desreializing to a List and then converting it map.
    – MIX DML
    Aug 31, 2022 at 11:14
  • The JSON starts with { so is an object, not an array. You therefore need to use Map<String, Object> as the type for this untyped deserialization.
    – Phil W
    Aug 31, 2022 at 11:15

1 Answer 1

3
Edit

As @PhilW said in his comment: "If none of the property names clashes with an apex reserved word it is IMHO far better to used typed (rather than untyped) deserialization." I agree.

This is very well explained in the SFSE Canonical Q&A, How do I get started working with JSON in Apex?

My answer assumes that for whatever reason the OP needs untyped deserialization, and thus addresses the original question.


Welcome to Salesforce Stack Exchange (SFSE)!

This is one of those cases where the error correctly tells you what the issue is.

Looking at the example in the documentation here: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Json.htm#apex_System_Json_deserializeUntyped we see...

String jsonInput = '{\n' +
    ' "description" :"An appliance",\n' +
    ' "accessories" : [ "powerCord", ' + 
      '{ "right":"door handle1", ' + 
        '"left":"door handle2" } ],\n' +
    ' "dimensions" : ' + 
      '{ "height" : 5.5 , ' + 
        '"width" : 3.0 , ' + 
        '"depth" : 2.2 },\n' +
    ' "type" : null,\n' +
    ' "inventory" : 2000,\n' +
    ' "price" : 1023.45,\n' +
    ' "isShipped" : true,\n' +
    ' "modelNumber" : "123"\n' +
    '}';
    
Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(jsonInput);

You should change your first line to something like

Map<String, Object> dataMap = (Map<String, Object>)JSON.deserializeUntyped(jsonStr);

Then depending on what you are ultimately trying to do, you may need to change the rest of your code to use the map.

You should then be able to access this way:

String certName = dataMap.get('cert_name'); // certName == 'MCS'
String statusValue = dataMap.get('status'); // statusValue == 'success'
Integer powerValue = dataMap.get('power');  // powerValue = 390405;
...
1
  • 3
    If none of the property names clashes with an apex reserved word it is IMHO far better to used typed (rather than untyped) deserialization.
    – Phil W
    Aug 31, 2022 at 11:53

You must log in to answer this question.

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