JSON to V Struct Generator

Generate V language struct definitions from JSON data with proper type inference, option types for nullable fields, and json.decode example code.

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

  1. Enter a module name in the input field — "main" for executable programs, or a custom name for library modules
  2. Toggle "Use option types for nullable fields" to map JSON null values to ?Type syntax in V
  3. Toggle "Generate json.decode example" to include a working main() function that demonstrates deserialization
  4. Paste your JSON data into the input area on the left
  5. Click "Generate V Structs" and copy the output into a .v file in your project

Why Use This Tool?

Accurate type inference maps JSON values to the correct V types — string, int, f64, bool, and arrays
Option types (?Type) explicitly represent nullable fields, preventing runtime panics from missing data
Nested objects are extracted into separate struct definitions with proper field alignment
The json.decode example provides a ready-to-run starting point for JSON deserialization in V
Module declaration and json import are included automatically so the output compiles without modifications

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.

Input
{
  "id": 1,
  "name": "Alice",
  "email": "[email protected]",
  "is_active": true,
  "score": 98.5,
  "tags": ["developer"],
  "address": {
    "street": "123 Main St",
    "city": "Springfield"
  }
}
Output
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.

Input
{
  "username": "bob",
  "avatar_url": null,
  "bio": null,
  "followers": 42
}
Output
module main

import json

struct Root {
	username   string
	avatar_url ?string
	bio        ?string
	followers  int
}

fn main() {
	data := json.decode(Root, '...')!
	println(data)
}

Related Tools