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
Parag DevghareParag Devghare 

How to convert List<Object> into String.

Hi Trailblazers,
I have List<Object> and I would like to print the value as a String.

I tried something like this but not succeed:
List<Object> animals;
string resultAnimalName = string.valueOf(animals.get('name'));//this line is giving error
System.debug(resultAnimalName);

can you correct me if I had done anything wrong?
Thanks,
Parag Devghare
Best Answer chosen by Parag Devghare
Malika Pathak 9Malika Pathak 9

 Hi Parag,
Please find the solution

List<account> accList= [select name from Account Limit 1]; 
String str = JSON.serialize(accList);
system.debug('str>>>>>>>----- '+str);

If you find this solution is helpful for you please mark best answer

All Answers

SwethaSwetha (Salesforce Developers) 
HI Parag ,
Check out this post https://salesforce.stackexchange.com/questions/106567/how-to-convert-listobject-into-liststring that should help you get started.

Looks like you are doing trailheads. The below articles would come handy:
https://salesforce.stackexchange.com/questions/171161/create-an-apex-class-that-calls-a-rest-endpoint-and-write-a-test-class-trailhe
https://gist.github.com/mannharleen/2890d40f5cea62d058d62f7c16e39f73
https://developer.salesforce.com/forums/?id=906F0000000MIjRIAW

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
Malika Pathak 9Malika Pathak 9

 Hi Parag,
Please find the solution

List<account> accList= [select name from Account Limit 1]; 
String str = JSON.serialize(accList);
system.debug('str>>>>>>>----- '+str);

If you find this solution is helpful for you please mark best answer
This was selected as the best answer
Parag DevghareParag Devghare
Thanks Swetha for your input. But I wanted to know specifically 'Print string out of List<sObject>'. 
Also thanks to Malika for giving your input, that woranswers my qu.

Kind Regards,
Parag Devghare