+ Start a Discussion
SandrineSandrine 

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 Sandrine
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
SandrineSandrine
Howcan i create the nested responce