What is JSON to V Struct Generator?
V is a statically typed, compiled programming language designed for building maintainable and performant software with a syntax inspired by Go and Rust. V emphasizes simplicity, safety, and fast compilation — the entire V compiler itself is built in V and compiles in under a second. This generator converts your JSON data into V struct definitions with proper type inference, mapping JSON strings to V string, integers to int, decimals to f64, and booleans to bool. Nullable fields are handled with V option types using the ?Type syntax, which explicitly represents values that may be none. The tool also generates a json.decode example showing how to deserialize JSON strings into your V structs using the built-in json module.
How to Use
- Enter a module name in the input field — "main" for executable programs, or a custom name for library modules
- Toggle "Use option types for nullable fields" to map JSON null values to ?Type syntax in V
- Toggle "Generate json.decode example" to include a working main() function that demonstrates deserialization
- Paste your JSON data into the input area on the left
- Click "Generate V Structs" and copy the output into a .v file in your project
Why Use This Tool?
Tips & Best Practices
- V uses snake_case for field names by convention — the generator preserves your original JSON key names
- Option types in V use the ? prefix syntax (e.g., ?string) and can hold either a value or none
- V defaults to f64 for floating-point numbers, which matches most JSON number use cases
- Arrays in V use the []Type syntax — for example, []string for a list of strings
- The json.decode function requires the ! operator to unwrap the result or panic on error
Frequently Asked Questions
How are JSON types mapped to V types?
JSON strings map to string, integers to int, floats to f64, booleans to bool, null to ?Type (V option type), arrays to []Type, and nested objects to separate struct definitions.
When should I NOT use this generator?
Skip this tool if your JSON uses sum types (e.g., a field that can be either a string or a number) — V supports sum types but they cannot be inferred from a single JSON sample. Also, if you need custom JSON serialization attributes or embedded structs, those must be added manually after generation.
What is the ?Type option syntax in V?
In V, ?Type is an option type that can hold either a value of the given type or none. When "Use option types for nullable fields" is enabled, JSON null values are mapped to ?Type, making your structs explicitly represent the possibility of missing data. You can check for none using or blocks.
How does json.decode work in V?
V's json.decode function takes a struct type and a JSON string, returning an option. The ! operator unwraps it or panics on error. For example: data := json.decode(Root, json_string)! decodes the JSON into a Root struct instance. You can also use or for graceful error handling.
Can I change the module name in the generated code?
Yes, you can set any valid V module name using the module name input field. The default is "main", which is used for executable programs. Use a custom name if you are building a library module that will be imported by other V code.
Is my 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 struct with option types for nullable fields
Generate V structs from a JSON user object where some fields may be null.
{
"id": 1,
"name": "Alice",
"email": "alice@example.com",
"is_active": true,
"score": 98.5,
"tags": ["developer"],
"address": {
"street": "123 Main St",
"city": "Springfield"
}
}module main
import json
struct Address {
street string
city string
}
struct Root {
id int
name string
email string
is_active bool
score f64
tags []string
address Address
}
fn main() {
data := json.decode(Root, '...')!
println(data)
}API response with nullable fields using option types
A JSON payload with null values generates V option types for safe handling of missing data.
{
"username": "bob",
"avatar_url": null,
"bio": null,
"followers": 42
}module main
import json
struct Root {
username string
avatar_url ?string
bio ?string
followers int
}
fn main() {
data := json.decode(Root, '...')!
println(data)
}