You need to sign in to do that
Don't have an account?
Vishnu Prasad 18
Difference between JSON Parser and JSON Deserialization
Hi,
I am working on one of the requirement where I am receiving JSON string in the response. I am confused whether to use JSON Parser or JSON Deserialization. Can someone provide some details about these 2 methods to extract the value from JSON string.
Thanks,
Vishnu
I am working on one of the requirement where I am receiving JSON string in the response. I am confused whether to use JSON Parser or JSON Deserialization. Can someone provide some details about these 2 methods to extract the value from JSON string.
Thanks,
Vishnu
If you use JSON Parser method, you need to parse JSON manually. Check the below sample code for example.
By using JSON Deserialization, It is as simple as this. <Class/Object> is the Apex Class for parsing a JSON structure
For this, first we need to create a class or an Inner class, which would contain all the variables that are to be stored from the JSON.
The JSON2Apex is a very handy tool for this. Go to http://json2apex.herokuapp.com/ and paste the JSON String in the space provided, and click on create Apex.
This would give you a class with member variables from the JSON String. You can just use this class as is or you can paste it into your existing class like an inner class.
You then use the JSON.de-serialize to parse the JSON.
Please do nor forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
Best,
Nithesh
Thanks for the quick response. So, If I have understood it correctly, JSON Parser passes each and every element/token whereas using desrialization, we can get exact value of particular tag easily.
Thanks,
Vishnu
Please do nor forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
Best,
Nithesh
JSONParser parser = JSON.createParser(response.getBody());
<Class/Object> ResponseObject = (<Class/Object>) parser.readValueAs(<Class/Object>.class);
A JSON Parser is a tool or library that reads JSON-formatted data and converts it into a readable and usable format. The parser converts the JSON data into a data structure that can be easily processed by the programming language.
On the other hand, JSON Deserialization is the process of converting JSON data into an object in a programming language. This allows the programmer to access and manipulate the data in a more intuitive and organized way.
In short, a JSON Parser is a component of JSON Deserialization and is used to read and convert JSON data, while JSON Deserialization is the overall process of converting JSON data into a usable data structure in a programming language.
To understand the process of parsing JSON in Apex, visit the article at https://logcodes.com/parsing-json-in-apex-salesforce/, It offers a comprehensive guide to parsing JSON in Apex, with an example for better understanding.