JSON to C# Conversion
Convert JSON objects to C# classes instantly with intelligent property mapping.
Overview
The JSON to C# feature automatically generates C# classes from JSON data, saving time and reducing errors.
Basic Usage
Simple Conversion
Press Shift+F2
and enter:
{"name":"John","age":30}Person.cs
Generates:
namespace YourNamespace
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
With Folder
Create in a specific folder:
Models/{"name":"John","age":30}User.cs
Generates User.cs
in the Models folder.
Nested Structures
For nested folders:
Models/Dto/{"id":1,"name":"Product"}ProductDto.cs
Creates folders and file automatically.
Supported JSON Features
Primitive Types
- Strings:
"name": "value"
→string Name
- Numbers:
"age": 30
→int Age
- Decimals:
"price": 19.99
→decimal Price
- Booleans:
"active": true
→bool Active
Complex Types
Null Values
"value": null
→string? Value
(nullable reference types)
Arrays
"tags": ["tag1", "tag2"]
→List<string> Tags
"numbers": [1,2,3]
→List<int> Numbers
Nested Objects
{
"user": {
"name": "John",
"email": "john@example.com"
}
}
Generates two classes: main class and nested User class.
Mixed Arrays
Arrays with objects:
{
"items": [
{"id": 1, "name": "Item1"}
]
}
Generates Item class and List<Item>
property.
Generated Class Features
Property Naming
JSON keys are converted to Pascal Case:
"firstName"
→FirstName
"user_name"
→UserName
"email-address"
→EmailAddress
Type Inference
Types are intelligently inferred:
- Whole numbers →
int
- Decimals →
decimal
- ISO dates →
DateTime
- Arrays →
List<T>
- Nested objects → Custom classes
Code Quality
Generated code includes:
- Proper formatting
- Correct namespace
- XML documentation (optional)
- Nullable reference types
- Standard conventions
Advanced Features
Multiple JSON Objects
Convert multiple JSON objects:
{"id":1,"name":"User"}User.cs, {"id":1,"title":"Post"}Post.cs
Default Class Name
Without specifying a name:
{"name":"value"}
Creates RootObject.cs
by default.
Multiline JSON
For complex JSON, use the full editor:
- Create a file first
- Paste JSON
- Use command "Convert JSON to C#"
Use Cases
API Response Models
Convert API responses:
- Copy JSON response
- Use Quick Add with JSON
- Get C# models instantly
Configuration Classes
Convert config JSON to strongly-typed classes:
- Copy appsettings.json section
- Generate C# configuration class
- Use with Options pattern
Test Data
Generate test models from sample data:
- Create test JSON
- Convert to C# class
- Use in unit tests
Integration
With Solution Explorer
Generated classes:
- Appear immediately in tree
- Have correct namespace
- Are ready to use
- Integrate with IntelliSense
With Existing Code
- Can reference other types
- Work with dependency injection
- Serialize back to JSON easily
Tips
Better Type Inference
Provide sample data with:
- Realistic values
- All possible properties
- Varied data types
- Array with multiple items
Naming
Use descriptive class names:
UserDto.cs
for DTOsUserResponse.cs
for API responsesUserViewModel.cs
for view models
Organizing
Group related models:
Models/Api/{"..."}UserResponse.cs
Models/Dto/{"..."}UserDto.cs
Troubleshooting
Invalid JSON
If JSON is invalid:
- Check for missing quotes
- Verify comma placement
- Escape special characters
- Use online JSON validator
Wrong Types Generated
If types are incorrect:
- Provide better sample data
- Manually adjust generated class
- Use specific values (not null)
Namespace Issues
If namespace is wrong:
- Check folder structure
- Verify project root namespace
- Use namespace adjustment feature