0

I am getting an Error in Apex when i try to Deserialize the JSON Response. System.JSONException: No content to map to Object due to end of input

My JSON Response in debug Log is: [{"recId":"1"},{"recId":"2"}]

My Apex Class is :

public with sharing class myvalues {

 @AuraEnabled(cacheable=true)
 public static List<String> thisvalue(String searchKey , String selrec){
 system.debug('The value is '+selrec);
    If(selrec != null || selrec != ''){
    List<String> f = (List<String>) JSON.deserialize(selrec, 
      List<String>.class);
     system.debug('value of List is '+f);

     }
  }
}

What Am I missing Here?

1

The reason for your error is that selrec is not a list of strings, in Json terms, it is a list of objects, a list of strings would be ["abc","def"]

You need something like this:

class SelectRecord {
  String recId;
}

and then

  @AuraEnabled(cacheable=true)
  public static List<String> thisvalue(String searchKey , String selrec){
    system.debug('The value is '+selrec);
    If(String.isNotBlank(selRec)){
      List<SelectRecord> selectedRecs = (List<SelectRecord>) JSON.deserialize(selrec, 
         List<SelectRecord>.class);
      system.debug('value of List is '+selectedRecs);

    }
  }
| improve this answer | |
  • Hi cropredy. Thank You so much for explaining and it does make sense but I am still getting the same error after I change with your logic. Also I have changed the JSON response to : [{"recId":"abc"},{"recId":"def"}] – Padam Nov 1 '19 at 1:08
  • what does selRec look like in the debug statement once the method is entered? – cropredy Nov 1 '19 at 19:17
  • cropredy, the debug Statement is : The value is [{"recId":"abc"},{"recId":"def"}], I am doing JSON.Stringify in LWC javascript and then is getting this String – Padam Nov 3 '19 at 19:40
  • Hi cropredy, I have made some changes and have posted the same question here with some changes in Apex where I am getting the values if I hardcode the JSON to deserialize Vs dynamically getting the values salesforce.stackexchange.com/questions/283715/… – Padam Nov 4 '19 at 14:00

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.