This is exactly what is happening in the fullstack jsoo example in the Dream examples I think (minus the deriving JSON). Note that you’ll probably incur a bit of a performance hit by using OCaml JSON parsers in the browser rather than the built-in browser API for it.
If you are going to use js_of_ocaml in the frontend, you could look into Ocsigen, which is a fullstack solution which seamlessly integrates backend and frontend into a single codebase for both web and mobile targets. From the page:
(I can anecdotally confirm that the 3 directory structure works great ) But I want to bounce on @yawaramin recommendation by linking the Eliom PPX syntax extension webpage which clearly highlights why Ocsigen is the undisputed king of smooth client/server interaction! (also check their real tutorials, this is just a teaser)
(In case you can’t use Ocsigen, I liked their stuff so much that I stole their idea of interleaving client/server code in ppx_shared, but this is a dirt cheap hack and you’ll still have to handle the hard parts of client/server communication yourself)