I have tried to decode a json from a html response in Roc.
I could not find any json decoder in the standard library: https://www.roc-lang.org/builtins/ (not counting https://www.roc-lang.org/builtins/TotallyNotJson which I think is not available in the std).
I have found a nice package https://github.com/lukewilliamboswell/roc-json <3
But there is no option to parse only the fields I need. I have a response json with over a 100 fields, but I'm interested in only 3 of them!
And some of the fields have names like "abc:cde:efg:1" - I don't know how to model this in a Roc record.
And there is a second issue - DecodeError : [TooShort]
The DecodeError in the standard lib can only take "TooShort". And this way, the only Err result you can get from any Decoder is "TooShort".
How to check what's wrong with the decoded JSON, XML, etc ? E.g. a field is missing, a field is of a wrong type...
What I'm saying - it is really a pain to work with JSONs right now in Roc.
Will Roc have a builtin JSON decoder?
Is it possible to make the Decoder return a more specific error than just "TooShort" ?
How do you work with jsons right now? Am I missing something?
Yeah, the JSON implementation is not fully developed yet. There is no support for more "interesting" errors or record files that do not have a name that aligns with a record name. JSON is not intended to be available in the standard library (it used to be, but was pulled out into lukewilliamboswell's repo, which is what you have seen). It would be great to add all the features you've noted as missing - it's just a matter of capacity, and no one has gotten to them yet.
This may be useful for your "abc:cde:efg:1" issue.
I'm interested to know how you went @Artur Domurad and if you have any feedback to improve roc-json? I'm a bit late to your question, but hope it worked out for you.
To answer your question, you should be able to make a roc record and then use the custom mapping to relate this to your json. Happy to give you an example if that would help. here is a test that does something similar which may help.
Yes, that's a nice feature, and it seems to work for me.
But now I have a new issue - the json's fields are changing, sometimes couple of them are missing, and sometimes a few additional will appear.
The 3 or 4 fields that I am interested in are always there.
I have cloned your repo and now I'm trying to make your Json decoder just ignore the stuff that is not in the model/type.
But for now I'm just confused, and trying to figure out how the decoders and encoders work in the standard library :D
I'm pretty sure I know exactly where the problem is in here. It doesn't currently eat the bytes for the field that we don't care about, but it should. I haven't implemented this, but it should be as simple as looking up the first byte and then decoding that type, and throwing away the result, and carrying on with decoding the other fields.
I didn't know what to do at the time, but I'm reasonably confident I do know.
@Luke Boswell
I did make it work.
Not sure if my code is any good, but would like to try push my changes and create a PR.
Last updated: Jul 05 2025 at 12:14 UTC