What is JSON to Thrift Struct Generator?
Apache Thrift is an interface definition language (IDL) and binary communication protocol originally developed at Facebook for building scalable cross-language services. Thrift lets you define data structures and service interfaces in a neutral .thrift file, then generates code for over 20 programming languages including Java, C++, Python, Go, and Rust. This converter takes your JSON data and produces Thrift IDL struct definitions with proper type mapping, automatic nested struct extraction, optional field support, and namespace declarations. The generated .thrift file serves as a language-agnostic contract that ensures all services and clients agree on the data format, making it ideal for microservice architectures and RPC-based systems.
How to Use
- Paste your JSON data into the input area on the left
- Set the namespace declaration (e.g., java/com.example) to specify the target language and package
- Toggle "Include optional fields" to mark nullable JSON values as optional in the Thrift struct
- Toggle "Use camelCase field names" to convert snake_case JSON keys to camelCase Thrift field names
- Click "Generate Thrift IDL" and copy the output into a .thrift file in your project
Why Use This Tool?
Tips & Best Practices
- Large integer values exceeding 2,147,483,647 are automatically mapped to i64 instead of i32
- Nested JSON objects are automatically extracted as separate structs with proper references and dependency ordering
- Null values are mapped to optional fields when the option is enabled, matching Thrift best practices for nullable data
- Arrays are converted to list<Type> with the element type inferred from the first element in the array
- Use the namespace format "lang/package" (e.g., java/com.example) for language-specific package declarations
Frequently Asked Questions
How are JSON types mapped to Thrift types?
JSON strings map to Thrift string, integers map to i32 (or i64 for values exceeding 2,147,483,647), decimal numbers map to double, booleans map to bool, null maps to optional fields, arrays map to list<Type>, and nested objects map to separate struct definitions with proper references.
When should I NOT use this generator?
Avoid this tool if you need Thrift services (service definitions with methods), enums, unions, or typedefs — it only generates struct definitions. Also, if your JSON contains deeply recursive structures or circular references, the generator will not handle those correctly since Thrift does not support recursive types without manual intervention.
What is the difference between struct and exception in Thrift?
Thrift structs are general-purpose data containers for carrying information between services. Exceptions are specifically designed for error handling in service definitions — they can be thrown by service methods and caught by clients, similar to exceptions in programming languages. This tool generates structs only; you must create exception types manually.
How are arrays and nested objects handled?
JSON arrays are converted to Thrift list<Type> where Type is inferred from the first array element. Nested JSON objects are extracted as separate Thrift struct definitions and referenced by name in the parent struct. Dependencies are ordered so that nested structs appear before the structs that reference them.
What does the namespace declaration do?
The namespace declaration (e.g., namespace java com.example) tells the Thrift compiler which package or module to place the generated code in for each target language. You can specify multiple namespaces for different languages in the same .thrift file.
Is my JSON data sent to any external server?
No. All processing happens entirely inside your browser. Your JSON data never leaves your device, and no network requests are made during the conversion.
Real-world Examples
Defining a user service data contract
Convert a JSON user profile into a Thrift struct for a cross-language microservice API.
{
"id": 1001,
"username": "alice",
"email": "alice@example.com",
"is_active": true,
"score": 95.5,
"tags": ["admin", "developer"],
"profile": {
"bio": "Full-stack engineer",
"avatar_url": null
}
}namespace java com.example
struct Profile {
1: string bio,
2: optional string avatarUrl,
}
struct Root {
1: i32 id,
2: string username,
3: string email,
4: bool isActive,
5: double score,
6: list<string> tags,
7: Profile profile,
}Generating a product catalog schema for an e-commerce API
A product with nested pricing and dimensions produces multiple Thrift structs for a type-safe API contract.
{
"name": "Widget",
"price": {
"amount": 2999,
"currency": "USD"
},
"dimensions": {
"width": 10.5,
"height": 5.0,
"depth": 3.2
}
}namespace java com.shop
struct Price {
1: i32 amount,
2: string currency,
}
struct Dimensions {
1: double width,
2: double height,
3: double depth,
}
struct Root {
1: string name,
2: Price price,
3: Dimensions dimensions,
}