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
Sagar Hinsu 13Sagar Hinsu 13 

Convert sObject List to JSON

I know there are so many question which answers this type of questions but my problem is little different. 
I have one object called CRMData__c in that I have Name, CRMDefinition__c(master-detail), Number__c, Date__c fields. 
So CRMData__c will have many records with Date, Number__C some number, and CRMDefinition id. 

I am showing some data example below for better understanding. 
User-added image

I got the record list in one variable List<sObject> result = "My result";
based on this result I want a JSON like below.

 

{
    "Test1": 12,
    "Test2": 11,
    "date": "2018-01-01"
  },
  {
    "Test1": 15,
    "Test2": 23,
    "date": "2018-01-02"
  },
  {
   "Test1": 20,
    "date": "2018-01-03"
  }

Means based on the date it should take the values of Test1 data and Test2 data.. 
I tried so many methods but it gives me the "APEX CPU Time Limit" error.
Because first I have to loop through the dates.
 
sachin kadian 5sachin kadian 5
List<sObject> result = 'listOfYourRecords';
Map<Date,List<Integer>> mapOfDateWtihNumbers = new Map<Date,List<Integer>>();

loop through your records

for(record ....){
   if(!mapOfDateWtihNumbers.containsKey(record.date)){
      mapOfDateWtihNumbers.put(record.date,new List<Integer>());
   }
   mapOfDateWtihNumbers.get(recoed.date).add(record.Number);
}

//now you will have a map of Dates as keys and list of numbers as values

//you can use json serialize method to convert it to JSON if require

JSON.serialize(mapOfDateWtihNumbers);

If you need any other format JSON, you can loop through the map and convert it easily

Let me know if it helps you..
Sagar Hinsu 13Sagar Hinsu 13
@sachin
Thanks for the reply but this will give me date as a key json.. i want to iterate over the dates which are present in record and based on date it shoud create the json which i have mentioned in the question. 
sachin kadian 5sachin kadian 5
As you have map of dates and list of data related to them now, you can easily iterate over them and convert to JSON. 
 
Public class datawrapper{
    Public date record date;
    Public Integer test1;
    Public Integer test2;
}
 
//iterate map and create list of wrapper 

List<datawrapper> listofdata = new list<datawrapper>();

For (mapvalue){

//Create new wrapper record and add it to wrapper list


}

// At last serialize list of wrapper

JSON.serialize(listofdata)

   //if no of data attributes are not fixed, then you can iterate over map and create a json string directly instead of wrapper
Sagar Hinsu 13Sagar Hinsu 13
@sachin.. thanks again for your quick reply.. but Test1 and Test2 are lookup (master-details) field.. there can be many more like Test3, Test4 etc... here for example i just took Test1 and Test2.. 
Prasad N RPrasad N R
In my case, I tried a method similar to the one mentioned by Sachin Kadian 5. It is of the following format:
List<sObject> listOfData = Database.query(SOQLstatement);
JSON.serialize(listOfData);

Upvoted Sachin Kadian 5's answer.
V TV T

I found this and solved my problem.

string query='SELECT id, name from account';
string Outputget= JSON.serialize(database.query(query)); 
system.debug(Outputget);
 
The above syntax gives us following result:-
[
   {
      "attributes":{
         "type":"Account",
         "url":"/services/data/v48.0/sobjects/Account/your account ID"
      },
      "Id":"your account ID",
      "Name":"Test Account Name"
   }
]

https://salesforcetrail.blogspot.com/2020/05/get-salesforce-soql-result-in-json.html