Extract nested attribute value from JSON String using Apex

Salesforce offers two main ways to extract values from a JSON string
1) JSON.deserialize() This approach requires you to have a wrapper class/other valid entity to deserialize the data to.
2) JSON.createParser(jsonString) This approach requires you to loop through a bunch of confusing parser.nextToken() calls

In this article I am introducing an easy to use utility to extract inner attribute values from any JSON string. This is leveraging JSON.createParser(). But once you have this utility, you don't have to go through the parser.nextToken() confusion.

Example of use
Utils.extractAttributeFromJson('{"data":{"user":{"organization":{"id":"123"}}}}', 'data.user.organization.id');


Please note that it will be more performant to use JSON.createParser() directly if you have multiple attributes to extract from the JSON.

2 comments:

  1. Salesforce Training in Delhi
    https://cliqafriq.com/read-blog/225538_genius-how-to-figure-out-if-you-should-really-do-salesforce-training.html
    Salesforce Training in Delhi from APTRON is outlined to assist you with figuring out the intricacies of the innovation in a simple manner and set you up for modern requests. Being a Certified Salesforce expert can work on your resume and open a ton of vocation potential open doors. So utilize APTRON Salesforce training focus to benefit of what you want.

    ReplyDelete
  2. It will helpful to us thanks for your sharing.
    NareshIT

    ReplyDelete