I am reading this tutorial https://www.stackbuilders.com/blog/nonsense-getting-started-with-reason-and-reason-react/. One of the problems I am facing is that api.noopschallenge.com is now dead. I replaced the API call to this https://random-word-api.herokuapp.com/word?number=20 This works but returns a Json Array. I have to convert the Json Array to list(string).
I modified the decodeWords function as
let decodeWord = (json: Js.Json.t) : list(string) =>
switch(Js.Json.decodeArray(json)) {
| None => []
| Some(array) => Belt.Array.map(Js.Json.decodeString, array)
};
But this gives me error
This has type: Js.Json.t => option(Js.String.t) But somewhere wanted: array('a)
How do I convert the Json Array to list(string)?
Two problems:
Belt.Array.maparound.´array` should come first.decodeStringreturns anoption(string)instead of just astring, you'll have to deal with theNones somehow. UsingBelt.Array.keepMapis a shorter way of just ignoring them.But using the
Js.JsonAPI directly is rather cumbersome. You might want to consider using a third-party json decoding library such asbs-json(disclaimer: authored by me) instead. Then it would be as simple as:Or, if you still want it to return an empty list instead of raising an exception on decode failure: