JSON to Nim Type Generator

Convert JSON data to Nim type definitions with proper type mapping, object types, and JSON serialization annotations.

What is JSON to Nim Type Generator?

Nim is a statically typed systems programming language that compiles to C, C++, or JavaScript, offering the performance of low-level languages with an expressive, Python-like syntax. When working with JSON APIs or configuration files in Nim, you need type definitions that match the incoming data structure. This tool analyzes your JSON payload and generates Nim object type definitions with precise type mapping — strings become string, integers become int, floating-point numbers become float, booleans become bool, null values become Option[T], and arrays become seq[T]. Nested JSON objects are extracted into their own named types, following Nim's convention of PascalCase for type names and camelCase for field identifiers. Optionally, the tool can emit toJson and fromJson procs using Nim's standard json module, and wrap numeric fields in distinct types to prevent accidental value mixing at compile time.

How to Use

  1. Paste your JSON object or array into the input area
  2. Configure options: root type name, JSON serialization, distinct types
  3. Click "Generate Nim Types" to create the code
  4. Copy the output and add it to your Nim project

Why Use This Tool?

Automatic type inference from JSON values
Nested object type generation with correct ordering (children first)
Option[T] for nullable fields following Nim conventions
seq[T] for arrays with proper element type inference
JSON serialization procs (toJson/fromJson) using the standard json module
Distinct types for stronger type safety on numeric fields

Tips & Best Practices

  • Provide JSON that contains every possible field — absent keys cannot be inferred and will be missing from the output
  • Enable "Include JSON serialization" when you need round-trip parsing; the generated procs handle Option fields with isSome/isNone checks
  • Use distinct types when your domain has multiple integer or float concepts that should not be interchangeable, such as UserId vs OrderId
  • Nim naming conventions use PascalCase for types and camelCase for fields — the generator applies these automatically
  • If your JSON array contains heterogeneous item types, only the first element is sampled for type inference

Frequently Asked Questions

How are JSON types mapped to Nim types?

JSON strings map to string, whole numbers map to int, decimal numbers map to float, true/false map to bool, null maps to Option[T] where T is inferred from context (defaults to string), arrays map to seq[T] with the element type inferred from the first item, and nested objects become separate Nim object types named with PascalCase.

When should I avoid using this generator?

Skip this tool when your JSON schema is already defined in a .proto or .schema file — use a dedicated Protobuf or JSON Schema compiler instead. Also, if your API returns union types (e.g., a field that can be either a string or an object), the single-value inference here cannot represent that polymorphism accurately.

Is my JSON data sent to a server?

No. All type inference and code generation runs entirely in your browser using JavaScript. Your JSON data never leaves your device, making it safe to paste payloads containing sensitive values like API keys or personal data.

What does the {.jsonKey:"...".} pragma do?

When a JSON key uses snake_case or other naming that differs from the Nim camelCase field name, the generator adds a {.jsonKey:"original_name".} pragma. This tells Nim's json deserializer which JSON key maps to which field, so you can keep idiomatic Nim names while reading the original JSON format.

Why are nested types output before their parent types?

Nim requires that types be defined before they are referenced. The generator performs a topological sort, emitting child types first so that the parent object type can reference them without forward declaration errors.

Can I customize the generated code after copying it?

Absolutely. The output is plain Nim source code — you can add custom validation procs, change field visibility (using * export markers), add default values, or refactor types into separate modules as needed for your project.

Real-world Examples

REST API client for a user management service

When building a Nim HTTP client that talks to a user management API, you need Nim types that mirror the JSON responses. Paste a sample API response to generate object types and serialization procs, then use them with the standard HttpClient to parse responses directly into typed objects.

Input
{
  "userId": 42,
  "displayName": "Bob Smith",
  "email": "[email protected]",
  "isActive": true,
  "creditScore": 750.0,
  "roles": ["admin", "editor"],
  "lastLogin": null
}
Output
type
  Root* = object
    userId*: int
    displayName*: string
    email*: string
    isActive*: bool
    creditScore*: float
    roles*: seq[string]
    lastLogin*: Option[string]

Configuration file with nested settings

Nim applications often read JSON config files at startup. Generate typed config objects so the compiler catches misspelled keys and type mismatches before runtime, rather than discovering them through KeyError exceptions.

Input
{
  "database": {
    "host": "localhost",
    "port": 5432,
    "sslEnabled": true
  },
  "cache": {
    "ttlSeconds": 300,
    "maxEntries": 1000
  }
}
Output
type
  Database* = object
    host*: string
    port*: int
    sslEnabled*: bool
  Cache* = object
    ttlSeconds*: int
    maxEntries*: int
  Root* = object
    database*: Database
    cache*: Cache

Related Tools