JSON to Code

JSON to Haskell

Paste a JSON sample. Get Haskell types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.

Open the tool with Haskell chosen

An example

This JSON:

{
  "id": 42,
  "name": "Ada Lovelace",
  "active": true,
  "joined": "2020-01-02T03:04:05Z",
  "tags": ["engineer", "writer"],
  "address": { "city": "London", "postcode": null },
  "scores": [9.5, 8, 10]
}

becomes this Haskell:

{-# LANGUAGE StrictData #-}
{-# LANGUAGE OverloadedStrings #-}

data Root = Root
    { rootIDRoot :: Int
    , nameRoot :: Text
    , activeRoot :: Bool
    , joinedRoot :: Text
    , tagsRoot :: [Text]
    , addressRoot :: Address
    , scoresRoot :: [Float]
    } deriving (Show)

data Address = Address
    { cityAddress :: Text
    , postcodeAddress :: Maybe Text
    } deriving (Show)

decodeTopLevel :: ByteString -> Maybe Root
decodeTopLevel = decode

instance ToJSON Root where
    toJSON (Root rootIDRoot nameRoot activeRoot joinedRoot tagsRoot addressRoot scoresRoot) =
        object
        [ "id" .= rootIDRoot
        , "name" .= nameRoot
        , "active" .= activeRoot
        , "joined" .= joinedRoot
        , "tags" .= tagsRoot
        , "address" .= addressRoot
        , "scores" .= scoresRoot
        ]

instance FromJSON Root where
    parseJSON (Object v) = Root
        <$> v .: "id"
        <*> v .: "name"
        <*> v .: "active"
        <*> v .: "joined"
        <*> v .: "tags"
        <*> v .: "address"
        <*> v .: "scores"

instance ToJSON Address where
    toJSON (Address cityAddress postcodeAddress) =
        object
        [ "city" .= cityAddress
        , "postcode" .= postcodeAddress
        ]

and 5 more lines. Open the tool to see the whole file.

With the reader and the writer

The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.

{-# LANGUAGE StrictData #-}
{-# LANGUAGE OverloadedStrings #-}

module QuickType
    ( Root (..)
    , Address (..)
    , decodeTopLevel
    ) where

import Data.Aeson
import Data.Aeson.Types (emptyObject)
import Data.ByteString.Lazy (ByteString)
import Data.HashMap.Strict (HashMap)
import Data.Text (Text)

data Root = Root
    { rootIDRoot :: Int
    , nameRoot :: Text
    , activeRoot :: Bool
    , joinedRoot :: Text
    , tagsRoot :: [Text]
    , addressRoot :: Address
    , scoresRoot :: [Float]
    } deriving (Show)

data Address = Address
    { cityAddress :: Text
    , postcodeAddress :: Maybe Text
    } deriving (Show)

decodeTopLevel :: ByteString -> Maybe Root
decodeTopLevel = decode

instance ToJSON Root where
    toJSON (Root rootIDRoot nameRoot activeRoot joinedRoot tagsRoot addressRoot scoresRoot) =
        object
        [ "id" .= rootIDRoot
        , "name" .= nameRoot
        , "active" .= activeRoot
        , "joined" .= joinedRoot
        , "tags" .= tagsRoot
        , "address" .= addressRoot
        , "scores" .= scoresRoot
        ]

and 22 more lines. Open the tool to see the whole file.

What you need in your project

`aeson`.

The file holds a record for each object and the `FromJSON` and `ToJSON` instances for aeson.

The trap to know about

A field name that clashes with another record needs a prefix, because a plain Haskell record shares its field names across the module.

The options you can change

These belong to Haskell, so the row of switches changes with the language.

OptionWhat it doesDefault
Types onlyPlain types onlyfalse
Array typeUse Array or Listarray | list
Generated module nameGenerated module nameQuickType

Questions

How do I turn JSON into Haskell?

Paste your JSON into the box, choose Haskell, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.

Does my JSON go to a server?

No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.

What do I need to add to my Haskell project?

`aeson`.

How does it know which fields are optional?

From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.

Other languages

JSON to Elixir JSON to Elm JSON to Scala 3 All the guides

Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.

Open JSON to Code