Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

Can I parse Multi-Level nested JSON structure in APEX Salesforce, like we do in Java with GSON and Jackson libraries?

{

   "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.

1 Answer

0 votes
by (32.1k points)

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

Browse Categories

...