# \[ANN\] first release of ppx\_deriving\_jsonschema

**URL:** https://discuss.ocaml.org/t/ann-first-release-of-ppx-deriving-jsonschema/15320
**Category:** Community
**Tags:** announce
**Created:** [September 23, 2024, 2:02am UTC](https://discuss.ocaml.org/t/ann-first-release-of-ppx-deriving-jsonschema/15320 "2024-09-23T02:02:54Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Khady](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/khady/32/469_2.png) [@Khady](https://discuss.ocaml.org/u/Khady)
#### Post date: [September 23, 2024, 2:02am UTC](https://discuss.ocaml.org/t/ann-first-release-of-ppx-deriving-jsonschema/15320/1 "2024-09-23T02:02:54Z")

</div>

It is my pleasure to announce the first release of [ppx\_deriving\_jsonschema](https://ocaml.org/p/ppx_deriving_jsonschema/latest). Source repo is [GitHub - ahrefs/ppx\_deriving\_jsonschema](https://github.com/ahrefs/ppx_deriving_jsonschema/)

This small ppx should help you generate a (hopefully valid) json schema from an ocaml type.

Generally the derivation tries to produce a schema which looks natural, and that would also be compatible with the existing derivers for json out there. Basically you should be able to change the annotation to `[@@deriving jsonschema, yojson]` (or `json` instead of `yojson`) and to read/write json values that are matching the schema. There is a bit of tension on things like variants, which are represented as arrays by ppx\_yojson\_conv and ppx\_deriving\_yojson, but represented as enums by ppx\_deriving\_jsonschema. I plan to add a way to switch between the two behaviors soon.

```ocaml
type address = {
  street: string;
  city: string;
  zip: string;
} [@@deriving jsonschema]

type t = {
  name: string;
  age: int;
  email: string option;
  address: address;
} [@@deriving jsonschema]

let schema = Ppx_deriving_jsonschema_runtime.json_schema t_jsonschema

```

Will be turned into this schema

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "address": {
      "type": "object",
      "properties": {
        "zip": { "type": "string" },
        "city": { "type": "string" },
        "street": { "type": "string" }
      },
      "required": ["zip", "city", "street"]
    },
    "email": { "type": "string" },
    "age": { "type": "integer" },
    "name": { "type": "string" }
  },
  "required": ["address", "age", "name"]
}

```

Some more advanced functionalities are documented in the readme.

Please let me know if you see any important feature missing, if there are bugs, or if you have ideas of improvements.

This project was originally started during a Ahrefs dojo, in parallel to the ICFP conference in Milan, as a way to learn how to write a ppx. I can’t recommend enough [GitHub - pedrobslisboa/ppx-by-example: This repository contains examples to help on understanding what are and how to write PPXs in OCaml.](https://github.com/pedrobslisboa/ppx-by-example) to get going.

---

<div class="post-metadata">

### Author: ![Khady](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/khady/32/469_2.png) [@Khady](https://discuss.ocaml.org/u/Khady)
#### Post date: [September 26, 2024, 3:08am UTC](https://discuss.ocaml.org/t/ann-first-release-of-ppx-deriving-jsonschema/15320/2 "2024-09-26T03:08:43Z")

</div>

Released 0.0.2 on opam. It feels like the project is in a good shape now.

Changes:

- support for nativeint, bytes, ref, unit
- add ~variant\_as\_array for compatibility with ppx\_deriving\_yojson
- support variant payloads
- support polymorphic variants inheritance
- fix encoding of tuples
- change encoding of variants from enum to anyOf

I’m considering making `variant_as_array` the default in 0.0.3 as it would be more consistent with the ocaml ecosystem.

---

<div class="post-metadata">

### Author: ![Khady](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/khady/32/469_2.png) [@Khady](https://discuss.ocaml.org/u/Khady)
#### Post date: [March 8, 2025, 8:52am UTC](https://discuss.ocaml.org/t/ann-first-release-of-ppx-deriving-jsonschema/15320/3 "2025-03-08T08:52:46Z")

</div>

some demo of how it can be useful when working with chatgpt.

> **[OpenAI and structured outputs from OCaml](https://tech.ahrefs.com/openai-and-structured-outputs-from-ocaml-b198fcf701ca)**
>
> with magic, without (touching) JSON
