How to use the JSON to Apex Class Generator
- Paste a sample of the JSON you receive into the input, or drop a
.jsonfile onto it. - Set the root class name. This becomes the outer Apex class, for example
OrderResponse. - Choose options: a
parse()helper,@AuraEnabledfor Lightning components, or{ get; set; }properties. - Select Generate Apex, then copy the code or download it as a
.clsfile.
What JSON to Apex conversion means
Apex is strongly typed. To read a JSON payload comfortably you need classes whose properties match the payload’s keys, so that JSON.deserialize can fill them in. Writing those classes by hand is slow and error-prone for anything beyond a few fields. A JSON to Apex converter reads a sample payload and writes the classes for you.
Why Salesforce developers need wrapper classes
- REST callouts: parse the response of an external API into typed objects.
- Apex REST services: deserialize a request body sent to your
@RestResourceclass. - Lightning components: pass structured data between Apex and LWC with
@AuraEnabledproperties. - Platform events and webhooks: read JSON stored in a long text field.
Example
This JSON:
{
"name": "Acme",
"contacts": [
{ "email": "a@acme.example", "primary": true }
]
}becomes this Apex:
public class AccountPayload {
public String name;
public List<Contact> contacts;
public class Contact {
public String email;
public Boolean primary;
}
public static AccountPayload parse(String jsonString) {
return (AccountPayload) JSON.deserialize(jsonString, AccountPayload.class);
}
}Common conversion problems
Reserved words
Keys such as class, limit, date, new and object cannot be Apex property names. The generator appends _x and, when the parse() option is on, adds one line per renamed key that rewrites the key in the JSON text before deserializing. The rewrite only matches a quoted name followed by a colon, so values are never touched.
Keys with hyphens, spaces or leading digits
first-name becomes first_name and 2fa becomes x2fa. Double underscores are collapsed because Apex reserves them. These are mapped by parse() in the same way.
Numbers that are too large for Integer
An Apex Integer holds 32 bits. Values above 2,147,483,647, such as timestamps in milliseconds, are generated as Long. If one array item has a decimal point, the whole property becomes Decimal.
Null and empty values
When the sample contains null or [], the real type is unknown, so the property is generated as Object or List<Object> and listed under “things to check”. Paste a sample where the value is filled in, or correct the type by hand.
Names that collide
Apex is case-insensitive, so Name and name in the same object would clash. The second one gets a numeric suffix and is mapped by parse().
Features and limitations
What it does
- Nested objects become inner classes; arrays become
List<T>, including arrays of arrays. - Numbers are typed as
Integer,LongorDecimalfrom how they are written, so1and1.0differ. - Objects in an array are merged, so a key that appears in only some items is still included.
- Reserved words and invalid names are renamed, and the generated
parse()method maps them back. - Identical shapes share one class; different shapes with the same key get distinct names.
- Clear error messages with the line and column of the first JSON syntax problem.
What it does not do
- Types come from one sample. A
nullvalue or an empty array has no type to infer and becomesObject. - Dates are left as
String. JSON has no date type and formats vary between APIs. - It does not create an Apex test class or deploy anything to your org.
- Keys that depend on data, such as a map keyed by record ID, are generated as fixed properties. Use
Map<String, Object>for those. - Generated code is checked against Apex naming rules, not compiled. Review it before deploying.