# Yojson.t to/from Yojson.safe.t

**URL:** <https://discuss.ocaml.org/t/yojson-t-to-from-yojson-safe-t/9959>\
**Category:** Learning\
**Created:** [June 1, 2022, 7:22pm UTC](https://discuss.ocaml.org/t/yojson-t-to-from-yojson-safe-t/9959 "2022-06-01T19:22:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eelco\_Hoekema](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/eelco_hoekema/32/3697_2.png) [@Eelco\_Hoekema](https://discuss.ocaml.org/u/Eelco_Hoekema)\
**Post date:** [June 1, 2022, 7:22pm UTC](https://discuss.ocaml.org/t/yojson-t-to-from-yojson-safe-t/9959/1 "2022-06-01T19:22:17Z")

</div>

Hi,

I just started to create a simple json/http service in OCaml using Opium, just for fun and learning. One issue i bump into is the mapping between `Yojson.t` and `Yojson.Safe.t`. I did find a `Yojson.Safe.to_basic`, but that one provides me a `Yojson.Basic.t`, not a `Yojson.t`. And there is no to_safe or from_basic that gives me a `Yojson.Safe.t`from a`Yojson.Basic.t`.

I am quite new to OCaml, so i lack an understanding of common patterns. What is the usual approach for this?

```auto
This expression has type Yojson.Safe.t but an expression was expected of type Yojson.t

```

eelco

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [June 1, 2022, 7:49pm UTC](https://discuss.ocaml.org/t/yojson-t-to-from-yojson-safe-t/9959/2 "2022-06-01T19:49:27Z")

</div>

Hi, is there a specific reason you need to use `Yojson.t`? I’ve personally never had a use for that, I always use `Yojson.Safe.t` as the JSON data type.

---

<div class="post-metadata">

**Author:** ![Leonidas](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/leonidas/32/4039_2.png) [@Leonidas](https://discuss.ocaml.org/u/Leonidas)\
**Post date:** [June 2, 2022, 1:39pm UTC](https://discuss.ocaml.org/t/yojson-t-to-from-yojson-safe-t/9959/3 "2022-06-02T13:39:50Z")

</div>

I would also suggest to use `Yojson.Safe.t`, since `Basic` struggles with representation of integers.

One of the challenges going forward for Yojson will be to reduce the confusion between different variants of the parser, but for now as long as you use `Safe` you should be, excuse the pun, safe.

---

<div class="post-metadata">

**Author:** ![Eelco\_Hoekema](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/eelco_hoekema/32/3697_2.png) [@Eelco\_Hoekema](https://discuss.ocaml.org/u/Eelco_Hoekema)\
**Post date:** [June 3, 2022, 10:03am UTC](https://discuss.ocaml.org/t/yojson-t-to-from-yojson-safe-t/9959/4 "2022-06-03T10:03:09Z")

</div>

I guess that was part of the lack of understanding of common patterns. Just updating my types to Safe fixed my issues.

Thanks!
