# JSON Validation?

**URL:** <https://discuss.ocaml.org/t/json-validation/1671>\
**Category:** Learning\
**Created:** [March 6, 2018, 4:06pm UTC](https://discuss.ocaml.org/t/json-validation/1671 "2018-03-06T16:06:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabrik](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gabrik/32/876_2.png) [@gabrik](https://discuss.ocaml.org/u/gabrik)\
**Post date:** [March 6, 2018, 4:06pm UTC](https://discuss.ocaml.org/t/json-validation/1671/1 "2018-03-06T16:06:14Z")

</div>

Hi everyone,

I’m pretty new to OCaml, so sorry for my dumminess.  
I was looking for a way to validate some JSON String against a schema, I have see some libraries (yojson in particular), but I did not see any validation method.  
There is a simple way or I should implement validation myself?

Thanks!

---

<div class="post-metadata">

**Author:** ![tmeyer](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/tmeyer/32/890_2.png) [@tmeyer](https://discuss.ocaml.org/u/tmeyer)\
**Post date:** [March 6, 2018, 4:56pm UTC](https://discuss.ocaml.org/t/json-validation/1671/2 "2018-03-06T16:56:57Z")

</div>

Hi Gabriele,

Have you checked out the chapter on JSON in “Real World OCaml” yet?

See here:

[https://realworldocaml.org/v1/en/html/handling-json-data.html](https://realworldocaml.org/v1/en/html/handling-json-data.html)

I’d suggest reading it all, but particularly the section on “Automatically Mapping JSON to OCaml Types,” which shows how you can use ATDgen with Yojson to do quite a bit (including validation).

Good luck!

T

---

<div class="post-metadata">

**Author:** ![gabrik](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gabrik/32/876_2.png) [@gabrik](https://discuss.ocaml.org/u/gabrik)\
**Post date:** [March 6, 2018, 6:23pm UTC](https://discuss.ocaml.org/t/json-validation/1671/3 "2018-03-06T18:23:07Z")

</div>

Hi T,

Thank you very much, I have started reading about ATDgen, I’m using the version 2 of that book  
[https://dev.realworldocaml.org/15-json.html](https://dev.realworldocaml.org/15-json.html)

Thanks again!

---

<div class="post-metadata">

**Author:** ![gabrik](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gabrik/32/876_2.png) [@gabrik](https://discuss.ocaml.org/u/gabrik)\
**Post date:** [March 8, 2018, 11:53am UTC](https://discuss.ocaml.org/t/json-validation/1671/4 "2018-03-08T11:53:27Z")

</div>

Hi all,

Everything seems ok, except when I have some json that can contain different schemas in a value, for instance

```
{"type":1, "value":{"key1":"value1"}} 
or
{"type":2, "value":{"key_1231":12}} 

```

So in my case value can contain different json, I have tried using 'a and not defining so any type but I got an error when i try to compile the atd file.

Any suggestion?  
Thanks

---

<div class="post-metadata">

**Author:** ![mjambon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mjambon/32/6180_2.png) [@mjambon](https://discuss.ocaml.org/u/mjambon)\
**Post date:** [March 8, 2018, 6:45pm UTC](https://discuss.ocaml.org/t/json-validation/1671/5 "2018-03-08T18:45:25Z")

</div>

Unfortunately these situations happen a lot in the wild.

Atdgen doesn’t provide a good mechanism that can handle all the situations at the moment. I have now a good sense of what a good design would feel like but I have limited/no time to work on it.

So, right now the solution that needs to be implemented manually involves the following steps:

1. In the ATD file, define type1, type2, etc. as usual so they map directly between json and OCaml types.
2. Define a variant type to represent all the json messages that need to exist under the same type. The tags are chosen freely by you (e.g. `Type1` or something more meaningful).
3. [Write code to] read a json message first as a generic AST (returned by `Yojson.Basic.from_string` and alike).
4. [Write code to] inspect such tree and determine the tag and the value part (using pattern matching and `Yojson.Basic.Util`). You can rearrange the tree freely to produce and ATD-friendly structure for the value.
5. [Write code to] convert the value back to json (a string) and convert it to the desired OCaml type using the corresponding function generated by atdgen (e.g. `type1_of_string (Yojson.Basic.to_string value_ast)`).
6. Optionally, implement the reverse of steps 2-4 if you need to convert an OCaml message to json.

Here’s `example.atd`:

```auto
type type1 = { key1: string }
type type2 = { key2: int }

```

and `example.ml`:

```auto
(*
   Handle json messages like these:
     {"type":1, "value":{"key1":"value1"}}
     {"type":2, "value":{"key_1231":12}}
*)

type msg = [
  | `Type1 of Example_t.type1
  | `Type2 of Example_t.type2
]

(*
   Read a json message as an OCaml value of type `Example_t.msg`.
*)
let msg_of_string s =
  let open Yojson.Basic.Util in
  let ast = Yojson.Basic.from_string s in
  let type_ = ast |> member "type" |> to_int in
  let value_ast = ast |> member "value" in
  let value_json = Yojson.Basic.to_string value_ast in
  match type_ with
  | 1 -> `Type1 (Example_j.type1_of_string value_json)
  | 2 -> `Type2 (Example_j.type2_of_string value_json)
  | _ -> failwith "Unsupported message type"

```

---

<div class="post-metadata">

**Author:** ![gabrik](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gabrik/32/876_2.png) [@gabrik](https://discuss.ocaml.org/u/gabrik)\
**Post date:** [March 9, 2018, 7:56am UTC](https://discuss.ocaml.org/t/json-validation/1671/6 "2018-03-09T07:56:22Z")

</div>

Thank you, this seems a reasonable trade off, I’ll start to implement in this way, hoping everything goes well.

Many Thanks!
