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
AbAb 

constructing a nested JSON from the SOQL query

Hello,

I am making a SOQL quesry to object contact and lead.

From the values i get from the SOQL query, i will construct need to construct a JSON

I have multiple tree, like example below:
https://json.org/example.html
{
    "glossary": {
        "title": "example glossary",
		"GlossDiv": {
            "title": "S",
			"GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
					"SortAs": "SGML",
					"GlossTerm": "Standard Generalized Markup Language",
					"Acronym": "SGML",
					"Abbrev": "ISO 8879:1986",
					"GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
						"GlossSeeAlso": ["GML", "XML"]
                    },
					"GlossSee": "markup"
                }
            }
        }
    }
}

How can i construct a JSON from SOQL responce?
Best Answer chosen by Ab
Vishal_GuptaVishal_Gupta
Hi Sandrine,

You can use the standard JSON class to convert SOQL response into JSON, for ex :

List<Contact> lstContacts = [Select Id,Name,Account.name from Contact Limit 10];
System.debug(Json.serialize(lstContacts));

You can also get more details on following link 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm

All Answers

Vishal_GuptaVishal_Gupta
Hi Sandrine,

You can use the standard JSON class to convert SOQL response into JSON, for ex :

List<Contact> lstContacts = [Select Id,Name,Account.name from Contact Limit 10];
System.debug(Json.serialize(lstContacts));

You can also get more details on following link 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm
This was selected as the best answer
AbAb
Howcan i create the nested responce