• Lessons Home
Topics
Community
  1. Lessons
  2. Integration Fundamentals
  3. Working with JSON
  4. Parsing JSON

      Parsing JSON

      Parsing JSON

      A response body is a String until you parse it. JSON.deserializeUntyped turns that
      String into Apex values, and the shape of those values is not quite what you would
      guess from the JSON you sent.

      One Shape, Many Types

      The top level decides what the parser hands back: a JSON object becomes a
      Map<String, Object> and a JSON array becomes a List<Object>. Inside the map,
      every value is an Object, and each one carries the type the JSON gave it.

      Map<String, Object> product = (Map<String, Object>) JSON.deserializeUntyped(body);
      String title = (String) product.get('title');
      Boolean inStock = (Boolean) product.get('inStock');
      

      Numbers Arrive As Decimal

      Here is the part that catches people. A JSON number is ALWAYS a Decimal in Apex,
      whether it is 99 or 9.99 or 15.14. It is never an Integer, even when the
      number has no decimal point.

      Decimal stock = (Decimal) product.get('stock');
      Decimal price = (Decimal) product.get('price');
      

      Reading a number as an Integer fails at runtime, and the message points at the
      cast rather than at the JSON:

      Integer price = (Integer) product.get('price');   // System.TypeException
      

      The cast is only accepted when the number was written without a decimal point at
      all, so the very same line works for a stock count and blows up for a price. It
      also blows up for 100.0, because the number carries a decimal point even though
      its value is whole.

      Nested Objects Are Maps Too

      A JSON object inside the JSON object is another Map<String, Object>. You have to
      cast it before you can read its keys, because get on the outer map hands you an
      Object and not a map.

      Map<String, Object> product = (Map<String, Object>) JSON.deserializeUntyped(body);
      Map<String, Object> dimensions = (Map<String, Object>) product.get('dimensions');
      Decimal height = (Decimal) dimensions.get('height');
      

      A Missing Key Is Null

      get returns null for a key that is not in the JSON, and for a key whose value is
      JSON null. Nothing throws, so a null is easy to miss until you use it.

      Common Mistakes

      The three mistakes that cost the most time: reading a number as an Integer,
      reading a nested object without casting it to a map, and using a value straight out
      of get without casting it to the type you need. That last one bites hardest,
      because a Decimal in a comparison needs the cast as well, and an Object cannot
      be compared at all.

      Apex Code Editor
      Sign in to Submit

      Welcome to Lightning Challenge!

      How It Works

      • • Write your solution in the code editor
      • • Connect your Salesforce org to test
      • • Submit to check if your solution passes
      • • Use hints if you get stuck

      Note

      Complete this lesson challenge to earn points and track your progress. The code editor allows you to implement your solution, and the tests will verify if your code meets the requirements.

      Wally Assistant

      Wally can't hear you

      Please sign in to access the AI Assistant

      Sign In