function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael MMichael M 

How to get values from objects in List from JSON

Hello, I have a snippet of JSON that looks like this:
"dates": [
{
"name": "Service",
"qualifier": "472",
"date": "2020-09-29"
},
{
"name": "Plan Begin",
"qualifier": "346",
"date": "2020-09-01"
},
{
"name": "Issue",
"qualifier": "102",
"date": "2020-06-01"
}
],

I need to be able to get the values from each "object" individually. 
I am getting the list like this:

Map<String, Object> wholeResponse = (Map<String, Object>) JSON.deserializeUntyped(response2.getBody());
Map<String, Object> result = (Map<String, Object>) wholeResponse.get('result');
List<Object> lstdates = (List<Object>) result.get('dates');

But after that I am getting stuck. How would I be able to get the name, qualifier, and date from each object contained in this list? (This may be a basic question, but I am a newbie to json parsing:))
 
Best Answer chosen by Michael M
Maharajan CMaharajan C
Hi Micheal,

We have to use the for loop on lstdates then only we can access the  name, qualifier, and date from each object.

Here below for loop will run for three times.So, In each looping you can access the object details with help of Map<String, Object> inside for loop.

When the loop run first time you will see the name as Service in debug. In 2nd time the name is Plan Begin and in 3rd time name will be printed as Issue. So similarly we can see the other 2 values from object with the help of  dateattr.get('qualifier') && dateattr.get('date')

for (Object dt : lstdates) {
Map<String, Object> dateattr = (Map<String, Object>)dt;
     System.debug( ' name --> ' + dateattr.get('name') );    
     System.debug( ' qualifier --> ' + dateattr.get('qualifier') );
     System.debug( ' date --> ' + dateattr.get('date') );
 }

use the below link to understand the JSON untyped deserialization.
https://opfocus.com/json-deserialization-techniques-in-salesforce/#:~:text=The%20best%20way%20to%20discover,through%20the%20use%20of%20System.

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Micheal,

We have to use the for loop on lstdates then only we can access the  name, qualifier, and date from each object.

Here below for loop will run for three times.So, In each looping you can access the object details with help of Map<String, Object> inside for loop.

When the loop run first time you will see the name as Service in debug. In 2nd time the name is Plan Begin and in 3rd time name will be printed as Issue. So similarly we can see the other 2 values from object with the help of  dateattr.get('qualifier') && dateattr.get('date')

for (Object dt : lstdates) {
Map<String, Object> dateattr = (Map<String, Object>)dt;
     System.debug( ' name --> ' + dateattr.get('name') );    
     System.debug( ' qualifier --> ' + dateattr.get('qualifier') );
     System.debug( ' date --> ' + dateattr.get('date') );
 }

use the below link to understand the JSON untyped deserialization.
https://opfocus.com/json-deserialization-techniques-in-salesforce/#:~:text=The%20best%20way%20to%20discover,through%20the%20use%20of%20System.

Thanks,
Maharajan.C
This was selected as the best answer
Michael MMichael M
HI Maharajan,

In my use case, I am creating a new record based on this data. So I need to be able to get the 1st object from the list, and assign its values to 3 new fields, then take the 2nd object from the list and assign its values to different specific fields, and same wiht the 3rd object on the List. How would I do that?
Michael MMichael M
Ok i got it-- thank you!!!!
Karan UdawantKaran Udawant
get marathi status on Marathimulga.com
jessmin joejessmin joe
Some may have have special formats that allow us to represent the individual attributes as a single string. For example, a date can represent ...
https://now-gg-gta5.info/