I'm building a PATCH service where I'm trying to deserlize into a JSON payload that has different levels.

    "ShippingAddress": {
        "Country": "United States",
        "PostalCode": "80202",
        "StateOrProvince": "CO",
        "Street": "Testing Street 123",
        "City": "Denver"
    "BillingAddress": {
        "Country": "United States",
        "PostalCode": "80202",
        "StateOrProvince": "Colorado",
        "Street": "123 Main St 1122",
        "City": "Denver"
    "Person": {
        "LastName": "Dan20140214c",
        "IsPrimary": true,
        "FirstName": "Dan20140214c",
        "Email": "[email protected]",
        "Phone": "5511555555555",
        "MemberType": "Guest Traveler"

    "MobilePhone": "5555555555 ",
    "DryFlyAccountID": 120919,
    "ActivePURL": "",
    "AccountParentID": ""

I'm trying to put this into a Map<String, Object> castedBody = (Map<String, Object>)JSON.deserializeUntyped(requestBody); which works for everything else but I need some guidance on how to traverse the nodes inside the object. I have control over this service so I can flatten out the payload I'm just trying to make the payload as clean as possible.

When I deserialize it looks like this

{AccountParentID=, ActivePURL=, BillingAddress={City=Denver, Country=United States, PostalCode=80202, StateOrProvince=Colorado, Street=123 Main St 1122}, DryFlyAccountID=120919, MobilePhone=5555555555 , Person={[email protected], FirstName=Dan20140214c, IsPrimary=true, LastName=Dan20140214c, MemberType=Guest Traveler, Phone=9995959595}, ShippingAddress={City=Denver, Country=United States, PostalCode=80202, StateOrProvince=CO, Street=Testing Street 123}}

  • My personal preference is to keep JSON structures as flat as possible if they're representing a single object (as it looks like this does) to reduce the number of containsKey() checks you end up doing and the depth of your logic tree.
    – David Reed
    Jul 5, 2018 at 16:38

1 Answer 1


I would do the following

Create a custom ApexType that looks like:

public class Foo {
  ShippingAddress ShippingAddress;
  BillingAddress BillingAddress;
  Person Person;
  String MobilePhone;
  String DryFlyAccountId;
  String ActivePURL;
  String AccountParentId;

  public class ShippingAddress {
    String Country ;
  public class BillingAddress {
  public class Person {

and then

Foo foo = (Foo) Json.deserialize(thePatchBody,Foo.class);
  • That would work for me if it wasn't a PATCH service, they won't be sending me fields if they aren't updated.. When I deserialzie into that class it would use all the properties. It would give me null/blanks when in reality they just didn't send the field because it wasn't updated. When is why I'm using deserializedUntyped
    – EricSSH
    Jul 5, 2018 at 16:52
  • @EricSSH - ah true, I interpreted OP that if Foo.Person was null, then you knew that Person wasn't updated but otherwise a Person would be sent in its entirety.
    – cropredy
    Jul 5, 2018 at 17:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .