function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Deepak Singh 116Deepak Singh 116 

ystem.JSONException: Expected List<Railway.stations> but found "" at [line:1, column:2]

Hi,
Please suggest how to handle this type error in Apex.when a received blank value in response i get the error which is pasted below:-
Controller:-
public class Railway {
public string city            {get; set;}
public string result          {get; set;}
public JsonParser resset      {get; set;}

Public void invoke(){
string endpoint='https://indianrailways.p.rapidapi.com/findstations.php?station='+city;
Http http=new Http();
HttpRequest req=new HttpRequest ();
Req.setHeader('X-RapidAPI-Host', 'indianrailways.p.rapidapi.com');
Req.setHeader('X-RapidAPI-Key', 'b6aaeccc96msh12c221cffd99255p151d80jsn32a04d0da645');
req.setEndpoint(endpoint);
req.setMethod('GET');
HttpResponse res=http.send(req);
result=res.getBody();
if(result!=null || result!=''){
resset =(JsonParser)system.JSON.deserialize(result,JsonParser .class);
}
else
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter correct value'));
}
}

public class JsonParser{
        public list<stations> stations{get;set;}
    
    }
    public class stations{
        public String stationCode{get;set;}
        public String stationName{get;set;}
}
}

VF PAGE:-
<apex:page controller="Railway">
<apex:form >
<apex:inputText value="{!city}"/>
<apex:commandButton action="{!invoke}" value="Invoke" rerender="i"/> <br></br>
</apex:form>
<apex:pageBlock title="STATION NAMAE AND CODES" id="i">
<apex:pageBlockSection title="STATION NAMAE AND CODES" columns="4">
<apex:pageBlockTable value="{!resset.stations}" var="a"  rendered="{! !ISNULL(resset)}">
<apex:column headerValue="stationName" value="{!a.stationName}" />
<apex:column headerValue="stationCode" value="{!a.stationCode}"   />
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock> <br></br><br></br>
{!result} <br></br><br></br>
</apex:page>














ERROR:-
ystem.JSONException: Expected List<Railway.stations> but found "" at [line:1, column:2]
Error is in expression '{!invoke}' in component <apex:commandButton> in page irailway: Class.System.JSON.deserialize: line 15, column 1
Class.Railway.invoke: line 17, column 1
 
Best Answer chosen by Deepak Singh 116
Maharajan CMaharajan C
Hi Deepak,

Even if there is no Stations are found also you external system giving the below result in response body that is the reason if the condition getting passed:

{"stations":""}

so that i am checking further your response with below lines:

 Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(result);
 if(results.get('stations') != ''){
            List<Object> lststations = (List<Object>)results.get('stations');
            system.debug('@@@ lststations ==> ' + lststations.size());
            if(lststations.size() > 0){
            resset =(JsonParser)system.JSON.deserialize(result,JsonParser.class);
   }


If there is stations are found then you will get the response as below. you capture these this things in your debug log.
{"stations":[{"stationName":"TIRUNELVELI TOWN","stationCode":"TYT"},{"stationName":"TIRUNELVELI","stationCode":"TEN"}]}

Please mark this as best answer if it helps to you!!!

Thanks,
Maharajan.C

 

All Answers

Maharajan CMaharajan C
Hi Deepak,

I have made some of the changes in your class:

now  try the below code it will work.

public class Railway {
    public string city            {get; set;}
    public string result          {get; set;}
    public JsonParser resset      {get; set;}
    
    Public void invoke(){
        string endpoint='https://indianrailways.p.rapidapi.com/findstations.php?station='+city;
        Http http=new Http();
        HttpRequest req=new HttpRequest ();
        Req.setHeader('X-RapidAPI-Host', 'indianrailways.p.rapidapi.com');
        Req.setHeader('X-RapidAPI-Key', 'b6aaeccc96msh12c221cffd99255p151d80jsn32a04d0da645');
        req.setEndpoint(endpoint);
        req.setTimeout(12000);    // To Avoid read time out error
        req.setMethod('GET');
        HttpResponse res=http.send(req);
        result=res.getBody();
        system.debug('result ==> '+ result);
        if(result!=null && result!=''){
            system.debug('@@@ result ==> '+ result);
            Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(result);
            if(results.get('stations') != ''){
            List<Object> lststations = (List<Object>)results.get('stations');
            system.debug('@@@ lststations ==> ' + lststations.size());
            if(lststations.size() > 0){
            resset =(JsonParser)system.JSON.deserialize(result,JsonParser.class);
            }

            }
            else
            {
            resset = null;
            }

        }
        else
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter correct value'));
        }
    }
    
    public class JsonParser{
        public list<stations> stations{get;set;}
        
    }
    public class stations{
        public String stationCode{get;set;}
        public String stationName{get;set;}
    }
}

Thanks,
Maharajan.C
Deepak Singh 116Deepak Singh 116
Thanks Maharajan.
I have a query why followng check not working 

if(result!=null && result!='')

Because i am checking there that result string is '' or null then why code enters in block if the result in ''.
Maharajan CMaharajan C
Hi Deepak,

Even if there is no Stations are found also you external system giving the below result in response body that is the reason if the condition getting passed:

{"stations":""}

so that i am checking further your response with below lines:

 Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(result);
 if(results.get('stations') != ''){
            List<Object> lststations = (List<Object>)results.get('stations');
            system.debug('@@@ lststations ==> ' + lststations.size());
            if(lststations.size() > 0){
            resset =(JsonParser)system.JSON.deserialize(result,JsonParser.class);
   }


If there is stations are found then you will get the response as below. you capture these this things in your debug log.
{"stations":[{"stationName":"TIRUNELVELI TOWN","stationCode":"TYT"},{"stationName":"TIRUNELVELI","stationCode":"TEN"}]}

Please mark this as best answer if it helps to you!!!

Thanks,
Maharajan.C

 
This was selected as the best answer