# Yojson and ppx\_deriving\_yojson

**URL:** <https://discuss.ocaml.org/t/yojson-and-ppx-deriving-yojson/14047>\
**Category:** Learning\
**Tags:** ppx\
**Created:** [February 9, 2024, 1:37pm UTC](https://discuss.ocaml.org/t/yojson-and-ppx-deriving-yojson/14047 "2024-02-09T13:37:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashu414](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ashu414/32/5075_2.png) [@ashu414](https://discuss.ocaml.org/u/ashu414)\
**Post date:** [February 9, 2024, 1:37pm UTC](https://discuss.ocaml.org/t/yojson-and-ppx-deriving-yojson/14047/1 "2024-02-09T13:37:25Z")

</div>

code :

```auto
open Yojson

(* Define an OCaml type *)
type person = {
  name : string;
  age : int;
  } 
[@@deriving to_yojson]

(* Example person *)
let my_person = { name = "Alice"; age = 30 }
(* let xx = person_to_yojson my_person *)

(* Convert person to JSON and store in a file *)
let store_person_as_json_file filename person =
  let json_str = Yojson.Safe.to_string (person_to_yojson person) in
  let oc = open_out filename in
  output_string oc json_str;
  close_out oc

(* Usage *)
let () = store_person_as_json_file "person.json" my_person

```

while executing this I am getting error

```auto
dune build
File "bin/main.ml", line 36, characters 40-56:
36 | let json_str = Yojson.Safe.to_string (person_to_yojson person) in
                                             ^^^^^^^^^^^^^^^^
Error: Unbound value person_to_yojson

```

dune file

```auto
(executable
 (public_name cst_viewer)
 (name main)
 (libraries cst_viewer syntax yojson ppx_deriving_yojson))

(dirs syntax ml js_parser ext)

(env
 (dev
  (env-vars
   (CPPO_FLAGS -U=RELEASE)))
 (release
  (env-vars
   (CPPO_FLAGS -D=RELEASE))
  (ocamlopt_flags
   (:standard -O3 -unbox-closures)))
 (static
  (env-vars
   (CPPO_FLAGS -D=RELEASE))
  (ocamlopt_flags
   (:standard -O3 -unbox-closures))))

```

AIM : wanted to convert the custom type of ocaml to json. please let me know if I am doing anything wrong or if there’s any other way to convert OCaml custom types to Json?

---

<div class="post-metadata">

**Author:** ![K\_N](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/k_n/32/2793_2.png) [@K\_N](https://discuss.ocaml.org/u/K_N)\
**Post date:** [February 9, 2024, 1:52pm UTC](https://discuss.ocaml.org/t/yojson-and-ppx-deriving-yojson/14047/2 "2024-02-09T13:52:45Z")

</div>

You should also have a `preprocess` directive to invoke the pre-processor. Something like `(preprocess (pps ppx_deriving_yojson))` in the `executable` stanza (e.g. just after `libraries`).

Also, note that if the type you want to print is not defined by you but by some other library and the library itself does not provide the `to_json` function, you might need to  
vendor the code and modify it.

---

<div class="post-metadata">

**Author:** ![otini](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/otini/32/3533_2.png) [@otini](https://discuss.ocaml.org/u/otini)\
**Post date:** [February 9, 2024, 1:55pm UTC](https://discuss.ocaml.org/t/yojson-and-ppx-deriving-yojson/14047/3 "2024-02-09T13:55:24Z")

</div>

Actually, there is a trick to derive from types defined in other modules: [GitHub - ocaml-ppx/ppx\_deriving: Type-driven code generation for OCaml](https://github.com/ocaml-ppx/ppx_deriving?tab=readme-ov-file#working-with-existing-types)

---

<div class="post-metadata">

**Author:** ![K\_N](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/k_n/32/2793_2.png) [@K\_N](https://discuss.ocaml.org/u/K_N)\
**Post date:** [February 9, 2024, 2:16pm UTC](https://discuss.ocaml.org/t/yojson-and-ppx-deriving-yojson/14047/4 "2024-02-09T14:16:59Z")

</div>

Neat. But if the type is too complex or abstract, this won’t work too well. Since in another thread @ashu414 mentioned rescripts Parsetree (which I’m assuming is full of mutually recursive definitions) I wanted to warn in advance that it may be hard to achieve what they want.

---

<div class="post-metadata">

**Author:** ![otini](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/otini/32/3533_2.png) [@otini](https://discuss.ocaml.org/u/otini)\
**Post date:** [February 9, 2024, 2:33pm UTC](https://discuss.ocaml.org/t/yojson-and-ppx-deriving-yojson/14047/5 "2024-02-09T14:33:30Z")

</div>

Yeah, I agree with you.

---

<div class="post-metadata">

**Author:** ![emillon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/emillon/32/1206_2.png) [@emillon](https://discuss.ocaml.org/u/emillon)\
**Post date:** [February 9, 2024, 3:24pm UTC](https://discuss.ocaml.org/t/yojson-and-ppx-deriving-yojson/14047/6 "2024-02-09T15:24:01Z")

</div>

In addition to what @K_N said, also remove `ppx_deriving_yojson` from the `(libraries)` list. I think that Dune should warn about that (a ppx rewriter is not something you link against).

---

<div class="post-metadata">

**Author:** ![ashu414](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ashu414/32/5075_2.png) [@ashu414](https://discuss.ocaml.org/u/ashu414)\
**Post date:** [February 12, 2024, 8:19am UTC](https://discuss.ocaml.org/t/yojson-and-ppx-deriving-yojson/14047/7 "2024-02-12T08:19:24Z")

</div>

Thanks @K_N it’s working now

---

<div class="post-metadata">

**Author:** ![ashu414](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ashu414/32/5075_2.png) [@ashu414](https://discuss.ocaml.org/u/ashu414)\
**Post date:** [February 12, 2024, 10:42am UTC](https://discuss.ocaml.org/t/yojson-and-ppx-deriving-yojson/14047/8 "2024-02-12T10:42:56Z")

</div>

@emillon @K_N Facing one more issue, can you please help me  
Issue Thread : [Not able to use %{d}\_to\_yojson in different path](https://discuss.ocaml.org/t/not-able-to-use-d-to-yojson-in-different-path/14066)
