Easier Unit Testing JSON assertions?

I have these AWS Lambdas that return JSON. My unit tests validate usually a single property. However… even basic JSON requires the worst way of getting properties (see snippet below). I tried the whole thing["field"][0], but it doesn’t work because it’s Js.Json.t, not an Object. I’m using Jzon.encodeWith as the last line. Should I… be doing some other step to convert Js.Json.t to Object or something?

… and please judge me harshly on the below :crazy_face::

let result =
  Js.Json.decodeObject(json)
  ->Js.Option.getExn
  -> Js.Dict.get("loans")
  ->Js.Option.getExn
  -> Js.Json.decodeArray
  ->Js.Option.getExn
  -> Js.Array.unsafe_get(0)
  -> Js.Json.decodeObject
  -> Js.Option.getExn
  -> Js.Dict.get("merchantDiscountRate")
  -> Js.Option.getExn
  -> Js.Json.decodeString
  -> Js.Option.getExn
stringEqual(result, "0.7")

If you just want to get the value in a quick and dirty way, you can do something like this: Object | ReScript Language Manual

Doesnt make it much shorter but will keep you from raising Exception in 10 places if you use Belt.Option.flatMap/Js.Option.andThen instead? Seems like you should have a type for this thing and then parse json => option…then you can dig inner values as you please.

1 Like

Why not to use Jzon.decode->Result.isOk?

1 Like

… cause I’m a crackhead? lol, idk, I’ll do this, thanks!

1 Like