What is JSON to Julia Struct Generator?
Julia is a high-performance programming language designed for technical computing, and its struct system provides the primary way to define typed data structures. Unlike dynamically-typed languages, Julia structs carry explicit type annotations on every field (e.g., name::String, count::Int64), enabling the compiler to generate efficient machine code through multiple dispatch. This tool reads a JSON sample and generates Julia struct definitions with inferred type annotations — strings become String, integers become Int64, floats become Float64, booleans become Bool, and null values optionally become Union{T, Nothing} for safe nullable typing. Nested JSON objects produce separate struct definitions ordered children-first so the compiler sees dependencies before they are referenced. Optional integration with JSON3.jl and StructTypes.jl generates the boilerplate needed to parse JSON directly into your typed structs.
How to Use
- Paste a representative JSON object or array into the input area
- Configure options: root struct name, mutable vs immutable, module wrapper, JSON3.jl, StructTypes.jl, and Union{T, Nothing} for null
- Click "Generate Julia Structs" to produce the type-annotated Julia code
- Copy the output into a .jl file in your Julia project
- If using JSON3.jl, parse JSON with JSON3.read(json_string, RootStruct)
Why Use This Tool?
Tips & Best Practices
- Use realistic sample data for accurate type inference — the generator bases types on actual values
- Enable mutable struct if you need to modify fields after creation; otherwise prefer immutable for performance
- JSON3.jl is the recommended JSON library for Julia — enable it for parsing support with JSON3.read
- StructTypes.jl works with JSON3.jl for fine-grained control over field naming and serialization order
- For arrays of mixed types, the generator falls back to Vector{Any} — consider defining a Union type manually
Frequently Asked Questions
How are JSON types mapped to Julia types?
JSON strings map to String, integers to Int64, floats to Float64, booleans to Bool, null to Union{T, Nothing} (when enabled) or Any, arrays to Vector{T}, and nested objects to separate Julia struct definitions. The generator uses the actual values in your JSON sample to determine the correct Julia type for each field.
When should I NOT use Julia structs for JSON?
Avoid generating structs when your JSON schema changes frequently and you would need to regenerate types often. For exploratory data analysis with varying schemas, use Dict{String, Any} with JSON3.jl directly. Structs are best for stable, well-defined data shapes where type safety and performance matter.
What is JSON3.jl?
JSON3.jl is a fast, flexible JSON parsing and serialization library for Julia. It provides the JSON3.read function to parse JSON strings directly into Julia structs, and JSON3.write to serialize structs back to JSON. It is the recommended JSON library for Julia due to its performance and composability with StructTypes.jl.
What is StructTypes.jl?
StructTypes.jl is a companion package to JSON3.jl that provides the @StructType macro and other annotations for controlling how Julia structs are serialized and deserialized. It allows you to specify custom naming strategies, omit fields, and handle non-standard JSON structures.
What is the difference between struct and mutable struct?
In Julia, a struct is immutable — its fields cannot be changed after creation. A mutable struct allows field values to be modified. Use mutable struct when you need to update fields, and regular struct for immutable data which is generally preferred for performance and safety.
Is my data sent to a server?
No, all processing happens entirely in your browser. Your JSON data never leaves your device. No data is collected, logged, or transmitted to any server.
Real-world Examples
Scientific dataset type definitions
Generate Julia structs from a JSON sample of experimental measurements for a computational physics project.
{
"experimentId": 42,
"temperature": 298.15,
"pressure": 101.3,
"isCalibrated": true,
"notes": null
}using JSON3
struct Experiment
experimentId::Int64
temperature::Float64
pressure::Float64
isCalibrated::Bool
notes::Union{String, Nothing}
end
# Parse JSON into struct:
# data = JSON3.read(json_string, Experiment)Nested data models with StructTypes
Handle a JSON payload with nested location data, producing separate struct definitions with StructTypes annotations.
{
"city": "Tokyo",
"population": 13960000,
"coordinates": {
"lat": 35.6762,
"lon": 139.6503
}
}using JSON3
using StructTypes
StructTypes.@StructType Coordinates StructTypes.Struct
struct Coordinates
lat::Float64
lon::Float64
end
StructTypes.@StructType City StructTypes.Struct
struct City
city::String
population::Int64
coordinates::Coordinates
end
# Parse JSON into struct:
# data = JSON3.read(json_string, City)