What's the plan for JSON encoding/decoding? Will this be part of the standard library or should users import https://github.com/lukewilliamboswell/roc-json ? If it's the latter option, is someone working on migrating it?
Check out the built-in Json.parse. Mr. Json "Luke" Boswell brought it to my attention :ok:
Note that there's currently a leak on successful parses (https://github.com/roc-lang/roc/issues/9953), but I don't think that'd be an issue for most use-cases. Given the perf of the Roc org I also think that'll be resolved swiftly ;D
Oh wow, it works, so cool! :tada:
» numbers : Try(List(U64), _)
» numbers = Json.parse("[1,2,3]")
assigned `numbers`
» numbers
Ok([1, 2, 3])
Indeed, lovin' it! Roc is really the best.
I can encode a few things to JSON, but not everything. What am I doing wrong?
» Json.encode(123)
Ok("123.0")
» Json.encode(123.U8)
Ok("123")
» Json.encode("Hello, World!")
Ok("\"Hello, World!\"")
» Json.encode(Bool.True)
Ok("true")
» Json.encode([1, 2, 3]) # Error! Missing method.
» Json.encode({name: "Bob", age: 20}) # Error! Missing method.
» Json.encode(None) # Error! Missing method.
oh I know what that is. Can you open an issue for it?
Updated the http package today and there's a demo in there with the new syntax which I think is pretty cool :smile:
https://github.com/roc-lang/http/blob/main/examples/router.roc
I suspect there will be a more idiomatic way to setup CRUD style API's but the Json parsing/encoding machinery is all there.
Like I wonder if it will be more idiomatic to have a Thing as a nominal type and then inside that have a DTO style inner record and then the parser and encoding methods use that so it's all contained in a nice abstraction (probably also in a separate file for just that type module).
Richard Feldman said:
oh I know what that is. Can you open an issue for it?
Thanks @Richard Feldman , I opened https://github.com/roc-lang/roc/issues/9970
@Aurélien Geron ok, fixed! also I renamed Json.encode to:
Json.to_str_try (works the same way as the previous Json.encodeJson.to_str - doesn't return a Try, but you can't use it to encode F32 or F64 values, since those are the only types that can fail to encode at runtime (if they are NaN/Infinity/-Infinity)I know a lot of JSON serialization libraries silently encode nan/infinity/-infinity as null, but I don't want to do that as a silent default. If there's demand for it, we can always introduce an explicit convenience function that you can opt into which does that.
Last updated: Jul 23 2026 at 13:15 UTC