JSON to C# Class
Convert JSON into C# POCO class definitions with properties, data annotations, and Newtonsoft attributes.
About this tool
JSON to C# Class is a developer tool that automatically converts JSON data structures into C# POCO (Plain Old CLR Object) class definitions. When working with APIs, configuration files, or external data sources in .NET applications, having strongly-typed C# classes ensures type safety and enables IntelliSense autocomplete in Visual Studio. This tool eliminates the tedious manual work of defining classes by analyzing your JSON and generating production-ready C# code with proper data types.
Simply paste your JSON object into the input field and click the convert button. The tool analyzes the structure, detects data types, handles nested objects and arrays, and generates C# classes with appropriate property types, Newtonsoft.Json attributes for serialization, and optional nullable annotations. The generated classes can be immediately copied into your .NET project and used with popular JSON libraries like Newtonsoft.Json (Json.NET) or System.Text.Json.
This is especially useful for developers integrating REST APIs, processing configuration files, or deserializing API responses in C#. The generated code supports nested objects, optional properties, and custom attribute mappings, making it compatible with both framework versions and modern .NET applications.
Frequently Asked Questions
Code Implementation
import json
from typing import Any
def to_pascal_case(s: str) -> str:
return ''.join(w.capitalize() for w in s.replace('-', '_').split('_'))
def cs_type(value: Any, key: str) -> str:
if isinstance(value, bool):
return "bool"
if isinstance(value, int):
return "int"
if isinstance(value, float):
return "double"
if isinstance(value, str):
return "string"
if isinstance(value, list):
if value:
return f"List<{cs_type(value[0], key)}>"
return "List<object>"
if isinstance(value, dict):
return to_pascal_case(key)
return "object"
def generate_classes(data: dict, class_name: str, classes: list) -> None:
props = []
for k, v in data.items():
prop_name = to_pascal_case(k)
type_str = cs_type(v, k)
if type_str == "string":
type_str = "string?"
props.append(f' [JsonProperty("{k}")]')
props.append(f' public {type_str} {prop_name} {{ get; set; }}')
if isinstance(v, dict):
generate_classes(v, prop_name, classes)
classes.append(f"public class {class_name}\n{{\n" + "\n".join(props) + "\n}")
json_str = '{"name": "Alice", "age": 30, "active": true}'
data = json.loads(json_str)
classes = []
generate_classes(data, "Root", classes)
print("\n\n".join(classes))Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.