JSON to Apex Converter
A Visual Studio Code extension that converts JSON to Apex classes with automatic inner class generation and static parsing support.
Features
- Convert JSON to Apex classes with proper property types
- Automatic generation of inner classes for nested objects
- Static parse method using System.JSON.deserialize
- Built-in test method with sample JSON
- Support for arrays and nested objects
- Sample value comments for better documentation
- No explicit getters and setters (using direct property access)
Installation
- Open Visual Studio Code
- Go to the Extensions view (Ctrl+Shift+X or Cmd+Shift+X)
- Search for "JSON to Apex Converter"
- Click Install
Usage
- Open or create a JSON file in VS Code
- Select the JSON content you want to convert
- Right-click on the selected text
- Choose "Convert JSON to Apex" from the context menu
- Enter your desired Apex class name when prompted
- A new file will open with the generated Apex class
Example
Input JSON:
{
"name": "John Doe",
"age": 30,
"isActive": true,
"scores": [95, 87, 91],
"address": {
"street": "123 Main St",
"city": "New York"
}
}
Generated Apex class:
public class fromJSON {
public String name; //John Doe
public Integer age; //30
public Boolean isActive; //true
public cls_scores[] scores; //[95, 87, 91]
public cls_address address; //{street: 123 Main St, city: New York}
class cls_address {
public String street; //123 Main St
public String city; //New York
}
public static fromJSON parse(String json) {
return (fromJSON) System.JSON.deserialize(json, fromJSON.class);
}
static testMethod void testParse() {
String json = ' {'+
' "name": "John Doe",'+
' "age": 30,'+
' "isActive": true,'+
' "scores": [95, 87, 91],'+
' "address": {'+
' "street": "123 Main St",'+
' "city": "New York"'+
' }'+
' }';
fromJSON obj = parse(json);
System.assert(obj != null);
}
}
Features in Detail
Automatic Inner Class Generation
- Nested objects are automatically converted to inner classes
- Inner classes are prefixed with 'cls_'
- Properties maintain their original types
Static Parsing
- Includes a static parse method using System.JSON.deserialize
- Easy to use:
YourClass obj = YourClass.parse(jsonString);
Test Method
- Automatically generates a test method
- Includes sample JSON for testing
- Basic assertion to verify parsing works
Property Types
- String: for text values
- Integer: for whole numbers
- Decimal: for decimal numbers
- Boolean: for true/false values
- Custom arrays: for array values
- Inner classes: for nested objects
Requirements
- Visual Studio Code 1.85.0 or higher
- Salesforce development environment (for using the generated Apex classes)
Contributing
Feel free to submit issues and enhancement requests!