JSON to Clojure Map Generator

Convert JSON to Clojure defrecord definitions or map literals with keyword keys.

What is JSON to Clojure Map Generator?

Clojure treats data as first-class citizens — maps with keyword keys (`:name`, `:email`) are the idiomatic way to represent structured data, and `defrecord` provides named types with fixed fields when you need protocol implementation or performance benefits over plain maps. This generator transforms JSON into two Clojure representations: map literals with keyword-prefixed keys for immediate data use, or `defrecord` definitions for named types with field vectors. JSON keys become Clojure keywords (e.g. `"is_active"` becomes `:is_active`), arrays become vectors (`["a" "b"]`), `null` becomes `nil`, and nested objects become either nested map literals or separate `defrecord` definitions depending on the output format you choose. The generated code follows Clojure formatting conventions — no trailing commas, keyword keys, and proper indentation for nested structures.

How to Use

  1. Select the output format: defrecord for named type definitions with protocol support, or map literal for immediate data structures
  2. Paste your JSON data into the input area — the generator handles nested objects, arrays, and all primitive types
  3. Click "Generate" and copy the output into your Clojure or ClojureScript project
  4. For defrecord output, use (->RecordName field1 field2) to construct instances, or (map->RecordName {:field1 val1}) to construct from a map
  5. For map literal output, the result can be directly embedded in your source code or used with read-string for runtime parsing

Why Use This Tool?

JSON keys are automatically converted to Clojure keywords, enabling idiomatic (:key map) lookups that are faster than string-based access
defrecord output creates named types that implement clojure.lang.IRecord and can participate in protocol dispatch for polymorphic behavior
Map literal output produces ready-to-use data structures that can be embedded directly in source code without additional parsing
Nested objects are handled recursively — deeply nested JSON structures produce properly indented Clojure maps or separate defrecord definitions
All Clojure value types are preserved: booleans as true/false, null as nil, numbers as-is, and strings with proper quoting

Tips & Best Practices

  • Use defrecord when your data type needs to implement a Clojure protocol or when you want the JVM performance benefits of a named type (field access is faster than map lookups)
  • Use map literals for configuration data, API response fixtures, or any scenario where you need flexible keys that may vary between instances
  • Clojure keywords are functions — after conversion, you can look up values with (:email user) instead of (get user :email), which is both shorter and marginally faster
  • When using defrecord with nested objects, remember that the nested record type must be defined before the outer record that references it — the generator orders definitions children-first
  • ClojureScript interop: if targeting ClojureScript, defrecord works identically but map literals may need cljc reader conditionals if shared with Clojure

Frequently Asked Questions

How does JSON map to Clojure types?

JSON strings become Clojure strings ("hello"), numbers stay as numbers (42, 3.14), booleans become true/false, null becomes nil, arrays become vectors ["a" "b"], and objects become maps with keyword keys {:name "Alice"}. JSON keys are prefixed with : to create Clojure keywords.

When should I avoid using this generator?

Skip this tool if your JSON contains keys that are not valid Clojure keywords (e.g. keys starting with numbers or containing spaces) — the generator will create them, but they require special syntax to access. Also avoid it if you need Clojure spec definitions, as the generator produces data definitions only, not spec predicates.

What is defrecord and when should I use it over a map?

defrecord defines a named record type with a fixed set of fields. Records implement clojure.lang.IRecord and IPersistentMap, so they behave like maps but with faster field access via JVM generated bytecode. Use defrecord when you need protocol implementations, type-based dispatch, or when the same field set appears across many instances and you want the performance benefit.

Why use keyword keys instead of string keys?

Keywords are the idiomatic way to represent keys in Clojure maps. They are interned for fast equality checks, can be used as functions for map lookups (:name user), and are self-documenting — :email clearly signals intent compared to a string key "email". Most Clojure libraries and frameworks expect keyword keys.

How are nested objects handled in defrecord mode?

Each nested JSON object becomes a separate defrecord definition. The outer record's field references the nested record type by name. Definitions are ordered children-first so the compiler sees the nested record before the outer record that uses it.

Is my data sent to a server?

No. All conversion runs entirely in your browser. Your JSON data never leaves your device — no network requests, no server-side processing, and no data storage.

Real-world Examples

API response fixture for Clojure web app

Convert a JSON API response into a Clojure map literal for use as a test fixture or development seed data.

Input
{
  "id": 42,
  "username": "alice",
  "email": "alice@example.com",
  "roles": ["admin", "editor"],
  "profile": {
    "bio": "Full-stack developer",
    "location": "Berlin"
  }
}
Output
{
  :id 42
  :username "alice"
  :email "alice@example.com"
  :roles ["admin" "editor"]
  :profile {
    :bio "Full-stack developer"
    :location "Berlin"
  }
}

Configuration data as defrecord types

Transform a JSON configuration file into Clojure defrecord definitions for type-safe configuration handling.

Input
{
  "database": {
    "host": "localhost",
    "port": 5432,
    "name": "myapp"
  },
  "cache": {
    "enabled": true,
    "ttl": 3600
  }
}
Output
(defrecord Database [host port name])

(defrecord Cache [enabled ttl])

(defrecord Root [database cache])

Related Tools