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
laxmi narayan 11laxmi narayan 11 

Showing error when i run test class.

Hi there,

I am getting this error at the time of running test class at the below line.
      list<User> users = (list<User>) JSON.deserialize(newUsersJson, List<User>.class);
Error: malformed json exception.expected [ in salesforce at the begning of the list/set
any help can be appreciable.

Thanks
 
Best Answer chosen by laxmi narayan 11
Ajay K DubediAjay K Dubedi
Hi, laxmi

After insert user in userList in a test class, you have to serialize this list
String userDetails = JSON.serialize(userList);

After that pass userDetails to your class method for running
 list<User> users = (list<User>) JSON.deserialize(newUsersJson, List<User>.class);

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

All Answers

Himanshu.SFDCHimanshu.SFDC
Hi Laxmi,
As mentioned in error there is some issue with format of json. Try online tool https://jsonlint.com (https://jsonlint.com/) to validate your json then process.
Ajay K DubediAjay K Dubedi
Hi, laxmi

After insert user in userList in a test class, you have to serialize this list
String userDetails = JSON.serialize(userList);

After that pass userDetails to your class method for running
 list<User> users = (list<User>) JSON.deserialize(newUsersJson, List<User>.class);

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer
Deepali KulshresthaDeepali Kulshrestha
Hi Laxmi,
Greetings to you!
    
- The problem is that you're trying to deserialize a JSON object (not an array) into a list
- I sharing a reference link for the same problem : 

https://developer.salesforce.com/forums/?id=906F000000090abIAA 
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.