-1

I obtained some JSON output from a REST response and I want to parse that directly in my Apex script to either run if-tests or determine which JSON key contains information to run further actions. Salesforce doesn't seem to have good documentation here on how to do it, but I've seen references to a JSON2Apex module that can perform some of what I need. How can I learn how to properly parse JSON text responses to run follow-up actions? Is everything done by making calls to nextToken() or getCurrentToken() down the line until you find what you need?

2
  • Well, this will probably be a duplicate of many posts, but the problem remains is that there is very little documentation listing all the things you can do with Apex JSON in one central place like a book or SuperBadge to cover it all.
    – xorLogic
    Commented Aug 8, 2018 at 19:00
  • 2
    The format here is specific questions and answers. If you are looking for one central place to cover everything JSON can do in Apex, you've come to the wrong place. It's hard to tell exactly what you're asking, but it's almost always easier to use serialize/deserialize methods than parsers. You may want deserializeUntyped here.
    – Adrian Larson
    Commented Aug 8, 2018 at 19:04

1 Answer 1

3

There is a single page in the documentation, JSON Support, that has links to all of the various techniques you can use for serializing and deserializing JSON.

JSONParser is the the most involved method, because you're responsible for handling all the tokens correctly. It's much easier to use either of the automatic methods instead, JSON.deserialize and JSON.deserializeUntyped.

For example, you might do this:

Object base = JSON.deserializeUntyped(res.getBody());
if(base instanceOf List<Object>) {
  // Go through the list looking for a value
} else if(base instanceOf Map<String, Object>) {
  // Check the keys to see if it is present
}

You can do this recursively, if you desire:

Object findToken(Object struct, String key) {
  if(struct instanceOf List<Object>) {
    List<Object> values = (List<Object>)struct;
    for(Object value: values) {
      Object returnValue = findToken(value, key);
      if(returnValue != null) {
        return returnValue;
      }
    }
    return null;
  }
  if(struct instanceOf Map<String, Object>) {
    Map<String, Object> values = (Map<String, Object>)struct;
    return values.get(key);
  }
  return null;
}

I wrote a post a few years ago about an XmlToJSON parser that you might find relevant; instead of parsing JSON, it parses XML, but the principles of navigating through a complicated, recursive structure still apply. You can also find other stuff posted on the Internet; lots of people have written code to parse arbitrary (and no-so-arbitrary) JSON.

As you've noted, there's also JSON2Apex, which gives you a class to deserialize to. Doing this is probably the best way to do it, assuming you can get a sample of the JSON you're getting back, and it's some fairly standard JSON (I've seen really complicated that JSON2Apex can't handle, so fair warning...).

You might want to start with the documentation, though, in the end, because the information is there, it just happens to not be all on one page.

1
  • Thanks sfdcfox. This gives a good overview of the state of JSON in Apex and was exactly what I'm looking for. I'm not that well-versed in JSON parsing, but I only have a few simple keys I'm looking to find and confirm they're present. I think right now my biggest problem is conceptualizing how and what I need to reach my goal.
    – xorLogic
    Commented Aug 8, 2018 at 19:22

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