Back
Can I parse Multi-Level nested JSON structure in APEX Salesforce, like we do in Java with GSON and Jackson libraries?
JSON
APEX Salesforce
GSON
Jackson
{ "key1":"value1", "key2":{ "key3":"value3", "key4":{ "key5":"value5" } }}
{
"key1":"value1",
"key2":{
"key3":"value3",
"key4":{
"key5":"value5"
}
I have already explored this.
Parsing JSON in Apex Salesforce
But I want a generic solution which will parse any JSON to the required Apex Object.
You use the serialize/deserialize methods of the JSON class in Apex:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_class_System_Json.htm
You'll want to create an Apex class based on your input JSON, but there's even a handy tool to do it:
http://json2apex.herokuapp.com/
Regrettably, you're bounded by the depth of the Apex classes. I think after key4 in your example, you're back to parsing it manually.
One thing to consider is building a partial model in Apex, then hydrating any deeper nested objects you need using the parser.
To learn in-depth about Workflow in Salesforce, sign up for an industry based Salesforce Certification.
31k questions
32.8k answers
501 comments
693 users