Recommended way to decode a JSON

Hi :wave:

what is the recommended way to decode a JSON in ReScript?
Do I have to use a library like bs-json or decco or is there a way to do it in pure ReScript?

I also found this (https://rescript-lang.org/docs/manual/latest/json), but it seems to be a bit unsafe because there is no validation of the structure. I could be wrong on this though.

Thank you! :slightly_smiling_face:

1 Like

As far as I know those two libraries are the best way to do it if you care about validation.

If you don’t care about validation of course you can use the method in that link you shared, or do lots of naughty things with Obj.magic (which of course can be very dangerous from a code correctness point of view!).

I personally use decco, because it is so extremely easy to setup (just add [@decco] [@decco.decode] or [@decco.encode] above the type you want to decode to depending on your use).
[I’ve heard that decco can sometimes add extra code and be more heavy than bs-json, and since it is a ppx can slightly slow down your compile - just to list some of it’s downsides.]

There was a recent interesting discussion about this: Using Variants with data from a JSON http request

4 Likes

If you’re avoiding external dependencies, I wrote a generalized parsing library that would be easy to drop in if you want and applied it one of my hobby projects. I’m not good at this yet by any means, but I did attempt to explain my thinking.

3 Likes

Looks like an answer generated by ChatGPT. It’s outdated and a little bit harmful. Right now there are three main libraries for decoding JSON:

  • rescript-json-combinators if you need something lightweight. The successor of the mentioned bs-json
  • ppx_spice if you want generated decoders from types using ppx. The successor of decco
  • rescript-struct if you want an ultimate tool (with opt-in ppx support coming soon)

Also, in rescript@11 with new untagged variants you can easily decode and encode simple JSON without any library: Better interop with customizable variants | ReScript Blog

10 Likes

I strongly suggest you to not magically type JSON into structs without parsing it into typed data :pray:

As tempting as it is, the last thing you want is runtime type-errors that crash your app because the code (or the data) changed.

2 Likes

There is also rescript-json Coming from elm, I can say it resembles a lot the api of elm-json and keeps decoders/encoders very readable.

Just for readability comparison with rescript-schema, which is on the right, and rescript-json documentation on the left. Unless you’re building for a Cloudflare Workers, in my opinion, this is the best way to decode/encode JSON and even more.

6 Likes

For the record, rescript-json-combinators is very similar to elm-json, while also taking advantage of being written in rescript to make it significantly safer and more convenient in some (quite common) scenarios, such as decoding records.

Compare this decoder to the one above, for example:

  let decode = {
    open! Json.Decode
    object(field => {
      name: field.required("name", string),
      percent: field.required("percent", float),
      percentPer100K: field.required("percentPer100K", float),
    })
  }
8 Likes

rescript-json-combinators is also good :smiling_face: I recommend it to everyone who doesn’t like eval in rescript-schema.

2 Likes