Showing posts with label JSON Parsing. Show all posts
Showing posts with label JSON Parsing. Show all posts

Monday, December 16, 2019

System.JSONException: Apex Type unsupported in JSON: Object

You will receive this error when your parsing/deserialize  the JSON responses either user the JSON classes or JSON Parser classes.Please read the below points
  • When you deserialize the response into specified class type and if that class is having any field with data type "Object" you will receive this error.
  • When you any JSONToApex Converter or JSONToC# we will end up with this issue.
  • Please replace all the fields with Object data type into String/Integer etc..
Sample JSON:


{
  "item": [
    {
      "CONTRACT": 1001117406,
      "SOLD_TO": 10081120,
      "PURCH_NO_C":,
      "STREET": "1290 Hamner Ave",
      "STR_SUPPL1": "",
      "STR_SUPPL2":,     
    }
  ]
}

After Coverting JSONToApex:

public class Item
{
    public integer CONTRACT { get; set; }
    public integer SOLD_TO { get; set; }
    public object PURCH_NO_C { get; set; }
    public string STREET { get; set; }
    public string STR_SUPPL1 { get; set; }
    public object STR_SUPPL2 { get; set; }
}

public class RootObject
{
    public List<Item> item { get; set; }
}



Deserialize the JSON:

JSONParser parser = JSON.createParser('sampleJsonResponse');
 RootObject pr= (RootObject)parser.readValueAs(RootObject.class); 
//Here you will receive an error because the RootObject.class is having fields (PURCH_NO_C,STR_SUPPL2)  of type Object .

WorkAround:
Please change the wrapper class fields data type from Object to appropriate types as integer,double,string etc

public class Item
{
    public integer CONTRACT { get; set; }
    public integer SOLD_TO { get; set; }
    public string PURCH_NO_C { get; set; }
    public string STREET { get; set; }
    public string STR_SUPPL1 { get; set; }
    public string STR_SUPPL2 { get; set; }
}

public class RootObject
{
    public List<Item> item { get; set; }
}

Hope this helps you..Enjoy..!


Wednesday, August 23, 2017

How to Invoke the REST API from the Visual Force Page in Salesforce

Problem:
We usually making a lot of API calls from Salesforce to External systems to get the some data or to pass the some updates to External Systems.All these calls will be made from the Apex class to External systems. What if you have situation like where you need to make a call to external systems from the visual force page.

Solution:
No need to worry still we have option to make an REST API call from the page directly.How?As we already know that Salesforce has introduced a concept called AJAX ToolKit.This is an frame which will be used to make all the operations from JavaScript similar to APEX. With help of this tool kit you can make an REST API call from the Script in Visual Force Page

Flow Diagram:
 

Sample Source Code:

<apex:page standardController="Account">
<apex:includeScript value="//code.jquery.com/jquery-1.11.1.min.js" />
<script src="../../soap/ajax/40.0/connection.js" type="text/javascript"></script>

<script>
function makeAPICall() 
{
    var weblink = //Your API endpoint 'https://www.yourednpoint/';
 
    $j.ajax({
  url: weblink,
  type: 'GET', // Type POST or GET
  dataType: 'json',
  beforeSend: function(request) {
      // Add All your Headers Here if Any
   request.setRequestHeader('HeaderString','HeaderValue');
   //Sample Headers 
   request.setRequestHeader('Country', 'IND');
   request.setRequestHeader('Currency', 'INR');
  },

  crossDomain: true,
  
  //If Successfully executed 
  success: function(result) {
   
   //Response will be stored in result variable in the form of Object.Please use result variable for processing
    console.log('Response Result'+result); 
    
      //If you want to convert JSResponse Object to JSON response 
    var jsonResp = JSON.stringify(result));

  },
  
  //If any Error
  
  error: function(jqXHR, textStatus, errorThrown) {
   
   // alert('ErrorThrown: ' + errorThrown);
  }
    });
}
</script>
<apex:form>
  call javaScript method from here to make an api call invocation
</apex:form>
</apex:page>

Hope this helps you....Enjoy!
 

Friday, August 4, 2017

Parse the JSON Response in Salesforce Apex -Part 2

In this post I'm going to explain how to create a wrapper class in apex to parse the JSON response which having multiple key value pairs only.

JSON String:


{
    "AccID1" : {
        "contacts" : {
            "0055be4ce5802020100" : "CREATED",
            "0055be4ce5802020100" : "Open"
        },
  "cs" : {
            "f0055be4ce5802020100" : "CREATED",
            "09055be4ce5802020100" : "Completed"
        }
    },
    "AccID2" : {
        "contacts" : {
            "0055be4ce5802020100" : "SUCCESSFUL"
        }
    },
    "AccID3" : {
        "contacts" : {
            "0055be4ce5801020100" : "CREATED"
        },
        "cs" : {
            "f0055be4ce5802030100" : "Open",
            "f0055be4ce5802030100" : "Re-Open",
            "f0055be4ce5802030100" : "Closed"
        }
    },
 "AccID4" : {
        "contacts" : {
            "0055be4ce5801020100" : "Initiated"
        }
       
    }
}

Apex Class:

//Wrapper Class For Parsering 
public class ParsingTest
{
  public ParsingTest()
  {
        String jsonString='Pass your JSON String Here';
 Map<string,ContactsCSWrapper> apiRespone = new Map<string,ContactsCSWrapper>();
 apiRespone = (Map<string,ContactsCSWrapper>)System.JSON.deserialize(jsonString,Map<string,ContactsCSWrapper>.class);
 System.debug('apiRespone..'+apiRespone.keySet()); //Outputs AccID1,AccID2,AccID3,AccID4
  }
  
  public class ContactsCSWrapper 
 {
  public map<string, string> cs { get; set; }
  public map<string, string> contacts { get; set; }
 }
}


Hope this helps you....Enjoy!

Parse the JSON Response in Salesforce Apex -Part 1

In this post I'm going to explain how to create a Wrapper class to Parse the JSON Response in Apex which contains key and value pair in JSON.

JSON String:

{
 "status": "SUCCESS",
 "errors": [],
 "data": {
  "oldCaseStatus": "Case_CANCELLATION_success",
  "newCaseStatus": "Case_CANCELLATION_success",
  "NewCaseNumber": "3732gy12"
         }
}

Apex Class:

public class ParsingTest
{
  public ParsingTest()
  {
      String json='{"status":"SUCCESS","errors":[],"data":{"oldCaseStatus":"Case_CANCELLATION_success","newCaseStatus":"Case_CANCELLATION_success","NewCaseNumber":"3732gy12"}}';
      ParsingWrapper apiRespone = new ParsingWrapper();
      
      apiRespone = (ParsingWrapper)System.JSON.deserialize(json,ParsingWrapper.class);
      System.debug('apiRespone..'+apiRespone);
  }

 //Wrapper Class For Parsering 

  public class ParsingWrapper
  {
   public String status{get;set;}
   public Map<String,String> data{get;set;}
   public Cls_errors[] errors;
  }

  public class Cls_errors
  {

  }
}


Hope this helps you....Enjoy!