Summary
I'm having a lot of trouble deserializing a JSON file into apex. The problem is not in how to deserialize a JSON string, but rather what type of apex objects I should be using. I don't come from a strongly typed background, so this issue (ironically, perhaps) is always confusing for me.
My JSON file looks like this:
{
"digest":[
{
"id": "123",
"name": "",
"email": "",
"sendEmailBool": true,
"Opportunities":[
{
"id": "opp123",
"name": "",
"NewChatters":[
{
"author": "",
"message": ""
}
],
"NewTasks":[
{
"type": "",
"subject": ""
}
],
"UpdatedTasks": [
{
"type": "",
"subject": "",
"status": ""
}
],
"CompletedTasks": [
{
"type": "",
"subject": "",
"completedBy": ""
}
]
}
]
},
{
"id": "456",
"name": "",
"email": "",
"sendEmailBool": true,
"Opportunities":[
{
"id": "opp123",
"name": "",
"NewChatters":[
{
"author": "",
"message": ""
}
],
"NewTasks":[
{
"type": "",
"subject": ""
}
],
"UpdatedTasks": [
{
"type": "",
"subject": "",
"status": ""
}
],
"CompletedTasks": [
{
"type": "",
"subject": "",
"completedBy": ""
}
]
}
]
}
]
}
I'd love for this to just be a map(string, string), but of course this will deserialize to map(string, object). That's fine, but let's say I save this to some var:
Map<string, object> m = (Map<string, object>)JSON.deserializeUntyped(strBody);
object o = m.get('digest');
What do I do with 'o' now? I've tried looking up how to work with 'object' in apex, but if you try googling it, you get nothing. It's like we have an object class but there's nothing you can do with it.
Question One
What can I do with this object o? Is there a way that I can further retrieve the other data fields? How about modify them? There doesn't seem to be any getter/setter methods on it...
A different approach
Instead of trying to make this JSON file work, perhaps I can build a JSON file from the ground up.
As you see from the JSON, this is mostly just lists of sObjects. Perhaps I can just create map(string, sObject) and map(string, map(string,sobject)) and json serialize that. But this is where the strongly typed issue gets in the way.
As you can see, the JSON file has both sObjects and strings. For instance:
{
"id": "456",
"name": "",
"email": "",
"sendEmailBool": true,
"Opportunities":[
{
"id": "opp123",
"name": ""
}
}
We have strings, boolean and an Opportunity sObject all in one.
Question 2
Assuming there isn't much to do with the solution from Question 1, how can I approach this issue from building the JSON from the ground up? What is the typical method?
Thank you all for your help!
PS. I saw a response to this question: How to deserialize a JSON String to Apex. The highest voted answer suggested using a tool call json2apex. Looking at that, it just seemed like a quick hack instead of the elegant solution. Perhaps this is the solution?
String s = (String)m.get('Digest')
but what I would do is define an apex class that models your json structure (with appropriate member variables) and just deserialize directly into an instance of that class in one line of code