# How do Apex developers parse and map incoming JSON integration payloads into strongly typed objects?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, JSON Parsing, Apex Data Mappings

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-apex-developers-parse-and-map-incoming-json-integration-payloads-into-strongly-typed-objects

## Answer

**Basics:** Integrations send JSON strings. Apex needs typed objects to work with them safely. You define an Apex class (a **wrapper**) whose property names match the JSON keys, and `JSON.deserialize` fills it. Tools such as "JSON to Apex" generators help create the classes.

**How it works:**
1. Define wrapper classes (inner classes are fine) with properties matching the JSON key names and types.
2. Call `JSON.deserialize(body, OrderPayload.class)`; use `List<OrderPayload>` for arrays.
3. Unknown JSON keys are ignored by `deserialize`; `JSON.deserializeStrict` throws if a key is missing in the class.
4. For unpredictable structure use `Map<String,Object> m = (Map<String,Object>) JSON.deserializeUntyped(body);` and cast as you navigate.
5. For huge documents use `JSONParser` with `nextToken()` and read values one at a time.
6. Convert wrapper data to sObjects in a bulk-safe loop, then DML once.

**Advanced / deep dive:** Trade-offs: typed wrappers give compile-time safety and readable code but break on reserved words used as keys (for example `class`, `number`, `date` names or hyphenated keys), where you pre-process with `String.replace` or use untyped parsing. Dates and datetimes parse only in ISO 8601 formats. Large payloads count toward heap (6 MB synchronous, 12 MB asynchronous); a streaming `JSONParser` or processing in Queueable/Batch reduces memory. Always wrap parsing in try/catch for `JSONException` and `TypeException`, and log the raw payload for support. For output use `JSON.serialize` (with `suppressApexObjects` option) or `JSON.serializePretty`. Managed packages need `@JsonAccess` to allow subscriber code to serialize or deserialize package classes.

**Code / config example:**
```apex
public class OrderPayload {
    public String orderId;
    public Decimal total;
    public List<Line> lines;
    public class Line { public String sku; public Integer qty; }
}

public static List<Order_Line__c> parse(String body, Id orderRecId) {
    List<Order_Line__c> out = new List<Order_Line__c>();
    try {
        OrderPayload p = (OrderPayload) JSON.deserialize(body, OrderPayload.class);
        for (OrderPayload.Line l : p.lines) {
            out.add(new Order_Line__c(Order__c = orderRecId, Sku__c = l.sku, Qty__c = l.qty));
        }
    } catch (JSONException e) {
        // log e.getMessage() plus the payload for troubleshooting
        throw new IntegrationException('Bad payload: ' + e.getMessage());
    }
    return out;
}
```

**Hidden gotchas & must-remember points:**
- `deserialize` silently ignores extra keys; `deserializeStrict` does not.
- Missing keys become null, so null-check before use.
- Reserved words as JSON keys need renaming or untyped parsing.
- Large documents can exceed heap limits.
- Do not put DML or SOQL inside the parsing loop.
- Number types: use `Decimal` for money to avoid precision loss.

**Best practice:** Use typed wrappers with defensive null checks, keep parsing in a dedicated class, and test with realistic and malformed payloads.

---
Source: ForceChallenges · last updated 2026-11-02
