What is JSON to PureScript Type Generator?
PureScript is a purely functional programming language that compiles to JavaScript, emphasizing type safety and correctness at every level. Its type system distinguishes between newtype wrappers (which create distinct types for typeclass derivation and safe abstraction) and type aliases (which are merely shorthand for existing types). This tool inspects your JSON data and generates PureScript record type definitions with precise type inference: strings become String, integers become Int, floats become Number, booleans become Boolean, null values become Maybe types for explicit null safety, and arrays become Array with the element type inferred from the first item. Nested JSON objects are extracted into separate type definitions, and the tool automatically adds the Data.Maybe import when nullable fields are detected. The output is ready to use with JSON decoding libraries like argonaut-codecs, simple-json, or foreign-generic.
How to Use
- Select output format: newtype for distinct types with typeclass support, or type alias for lightweight shorthand
- Set the root type name for the top-level generated type
- Paste your JSON data into the input area and click "Generate"
- Review the generated types and add JSON decoder instances using your preferred library
- Copy the output into a .purs file in your PureScript project src directory
Why Use This Tool?
Tips & Best Practices
- Use realistic sample data for accurate type inference — empty arrays default to Array String
- Newtype wraps the record for type safety and enables custom typeclass instances (Show, Eq, DecodeJson)
- Maybe types handle nullable JSON fields explicitly: Just "value" or Nothing, with no runtime null surprises
- Combine with argonaut-codecs or simple-json for JSON decoding that matches these type definitions
- Type aliases are useful for quick prototyping, but newtype is preferred for production code where type distinctness matters
Frequently Asked Questions
How are JSON types mapped to PureScript?
JSON strings map to String, integers to Int, floats to Number, booleans to Boolean, null to Maybe type (e.g., Maybe String), arrays to Array type with inferred element type, and nested objects become separate record type definitions referenced by name.
When should I avoid using this generator?
Skip this tool if your JSON uses union types that PureScript cannot represent as simple records — for example, fields that can be either a string or an object require Either or custom sum types that this generator does not produce. Also avoid it when you need Row polymorphism or extensible records, since the generated types are concrete record definitions.
What is the difference between newtype and type alias?
A newtype creates a distinct type that wraps a record, providing type safety and allowing custom typeclass instances. A type alias is just a named shorthand for the record type — it does not create a new type, so two type aliases with the same structure are interchangeable, which can lead to accidental mixing.
How are null values handled?
JSON null values are mapped to PureScript Maybe types. For example, a nullable string field becomes Maybe String, which can be Just "value" or Nothing. This forces you to handle the null case explicitly at compile time, eliminating runtime null reference errors.
Can I use these types with JSON decoding?
Yes. The generated types work with PureScript JSON libraries. Use argonaut-codecs with generic deriving, simple-json for simpler record decoding, or foreign-generic for automatic derivation. You will need to write or derive DecodeJson and EncodeJson instances for newtype-wrapped records.
Is my data sent to a server?
No. All type inference and code generation happens entirely in your browser. Your JSON data is never transmitted to any server, so it is safe to paste payloads containing personal data, API keys, or other sensitive values.
Real-world Examples
Generating types for a PureScript web application consuming a REST API
When building a PureScript frontend that communicates with a JSON API, paste a sample API response to generate the corresponding PureScript types. Then derive DecodeJson instances to safely parse API responses with compile-time type guarantees.
{
"id": 1,
"name": "Alice",
"email": "[email protected]",
"is_active": true,
"tags": ["developer"]
}import Data.Maybe (Maybe)
newtype Data = Data
{ id :: Int
, name :: String
, email :: String
, is_active :: Boolean
, tags :: Array String
}Creating configuration types with optional fields
Configuration files often have optional fields represented as JSON null. This tool maps those to Maybe types, ensuring your PureScript code must explicitly handle the case where a config value is missing rather than failing at runtime.
{
"host": "localhost",
"port": 3000,
"debug": false,
"api_key": null,
"allowed_origins": ["http://localhost:8080"]
}import Data.Maybe (Maybe)
newtype Data = Data
{ host :: String
, port :: Int
, debug :: Boolean
, api_key :: Maybe String
, allowed_origins :: Array String
}