1

In general I know how to create a apex class to parse the JSON response in salesforce. But with the current system I am integrating has a weird node name format and i having issues with creating apex class.

Below is the sample HAL+JSON

{
   "TotalResults": 30,
   "TotalPages": 2,
   "e:Page": 1,
   "_links": "www.google.com",
   "e:itemlink":[
                  {"href":"link1"},
                  {"href":"link2"},
                  {"href":"link3"}
                ]
}

For the above JSON i usually create a apex class like this

public class linkJson{
public Integer TotalResults;    
public Integer TotalPages;  
public Integer e:Page;  //this throws error because of naming convention  
public String _links;   
public cls_e:itemlink[] e:itemlink; //this throws error because of naming convention
//this through error because of naming
class cls_e:itemlink {
        public String href; 
    }
    public static linkJson parse(String json){
        return (linkJson) System.JSON.deserialize(json, linkJson.class);
        }
    }

I appreciate any help in resolving this issue.

3 Answers 3

2

When you receive the inbound JSON string, you could convert those terms into a clean format using replace:

inboundBody.replace('e:page', 'halPage');
inboundBody.replace('e:itemlink', 'halItemLinkList');
....
public Integer halPage { get; set; }
public List<halItemLink> halItemLinkList { get; set; }
....
public class halItemLink {
    public String href { get; set; }
}

Of course if you need to send JSON to the external system, you'll need to convert your clean formatted names back to the e: format after serializing your object.

2

Try to use the deserializeUntyped(jsonString) method in the Apex JSON class. It allows JSON strings to deserialize objects without defining a type.

This decreases your overall codebase and makes life a lot easier.

Hopefully this helps!

2
  • Mitch has the right answer, and I've taken it one more step.
    – tggagne
    Commented Nov 25, 2017 at 12:35
  • @mitch-spano, i think your suggestion will also work for my issue, but for my current endpoint i am going with suggestion of David
    – user51395
    Commented Nov 26, 2017 at 17:03
0

Mitch has the right answer, and I've taken it one more step.

I created a class that wraps the dictionary in some fun protocol to make the map even more useful. At work this class is called FsbObjectMap.

It has constructors for sending it a Json string, an XML string, and a map. This makes the class easy to instantiate.

Next, I added the obvious map methods like containsKey(), size(), isEmpty(), isNotEmpty(), etc.

Then I added getters like getBoolean(), getDecimal, getString(), etc. We even added a few getters specific to our application like getDateYYYYMMDD() that returns a date for a string formatted as YYYYMMDD.

Borrowing from Smalltalk, we included ifAbsent methods. For instance: getString(string aKey, string, valueIfAbsent), that returns valueIfAbsent if the value at aKey is, well absent. Because it's salesforce, it also returns that value if the key is null or empty.

Lately, we've added a new feature that supports paths. Since a json object can include other objects (as XML can too), the library now allows us to get nodes inside of nodes using code like:

aFirstName = aJMap.getString('outerNode/innerNode/firstName');

To avoid having to write:

aFirstName = aJMap.getJObject('outerNode').getJObject('innernode').getString('firstName);

The SF Partner I used to work at, Xede Consulting Group, Inc., made the repo public that includes this class (but doesn't include the path thingy...).

Before posting a link to it I'll check with them first.

Oh, we made another enhancement. Our getList() method is guaranteed to always return a list whether a single object or a list of objects is at a particular node. Though uncommon (if not unheard of) in organic JSON, it is not unusual in JSON that is converted from XML or SOAP messages.

You must log in to answer this question.

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