Lean 4 (Meta)programming Cookbook

Creating JSON ObjectsπŸ”—

Contributors: Anirudh Gupta

In Lean, the Lean.Json type is an inductive type that represents the different types of values that can be in a JSON structure. You can find its definition in import Lean.Data.Json:

Creating JSON objectsπŸ”—

There are three main ways to create JSON objects in Lean.

1. Using the json% macroπŸ”—

The most convenient way to create literal JSON values is with the json% macro. It allows you to write JSON syntax directly in your Lean code.

def myJson : Json := json% { "name": "Bob", "age": 42, "isActive": true, "scores": [1, 2, 3] }

2. Using Json.mkObjπŸ”—

You can manually build a JSON object using Json.mkObj which takes a list of key-value pairs (as String Γ— Json).

def manualJson : Json := Json.mkObj [ ("name", "Bob"), ("age", 9) ]

3. Using custom structures with Deriving ToJsonπŸ”—

The most idiomatic way to handle JSON in Lean is by defining a structure and deriving a Lean.ToJson instance. This allows you to convert Lean objects directly to JSON using the toJson function.

structure User where name : String age : Nat isAdmin : Bool deriving ToJson, FromJson def userJson (user : User) : Json := toJson user {"name": "Bob", "isAdmin": false, "age": 7}#eval userJson { name := "Bob", age := 7, isAdmin := false }