How to manipulate JSON in OCaml?

Use PPX Deriving JSON. PPX is a syntax extension and PPX Deriving JSON derives functions when annotating a type to convert from and to JSON.

1
2
3
4
type t = {
  name: string;
  age: int;
} [@@deriving yojson]
1
2
3
4
5
6
7
8
(library
 ((name test)
  (public_name test)
  (modules (test))
  (libraries (yojson ppx_deriving ppx_deriving.runtime
              ppx_deriving_yojson.runtime))
  (preprocess (pps (ppx_deriving ppx_deriving_yojson)))
))

Leave a Reply

Your email address will not be published. Required fields are marked *