Is there a way to use a type alias or something like the following for a record's key name? In this example UserId can be a random Str
UserId : Str
User : {
UserId : UserDetail
}
Wouldn't that be a use case for a hashmap/dict? (I don't recall what it's called in Roc.) Where the UserId
is the key and the UserDetail
is the value. IIRC records are for fixed keys/values.
Yeah, you want a roc Dict
Dict UserId UserDetail
https://www.roc-lang.org/builtins/dict
Perhaps I misunderstood but I assumed Seth wanted something like this:
UserId : Str
User : {
userId : UserId
}
myUser : User
myUser = { userId: "123" }
So the root of the question comes from trying to decode JSON. Something that looks like this where "userid123" can be a random string of letters/numbers
{
"users": {
"userid123": {
"lastLogin": "2023-03-11T03:38:05.007Z",
}
}
}
I just tried using Dict but Dict does not implement Decoding
Can you decode into a List and then convert that to a Dict?
Looking at the Dict docs @Brendan Hansknecht linked, I think you'd want something like
Dict.fromList [ T userId userDetails, ..... ]
Oh, is your json in a static format? Like always same keys with same types?
If so, it should be decoded into a record preferably. If not, a Dict is what you would want. That said, I don't think anyone has written code for decoding something into a dictionary or similar.
hmm ok. I'll try and play around with it some more later and see if I can come up with something
Decoding to a List first and then calling Dict.fromList
seems fairly reasonable as glancing at how Elm's Json.Decode.dict
works this is what it does.
I wasn’t able to conjure something up for this…
Are you able to decode to a List? I don't have a Roc setup in front of me at the moment so trying to do this all in my head :sweat_smile:
@Luke Boswell you were recently working on json decoding right? Any examples you can share and ideas how this would work out. I haven't messed with json decoding yet and don't know how it looks on roc.
Wolfgang Schuster said:
Are you able to decode to a List? I don't have a Roc setup in front of me at the moment so trying to do this all in my head :sweat_smile:
I was not, I'm familiar enough with roc yet . This was my first mini project to see if I could convert one of my node scripts into roc
Seth Workman said:
Wolfgang Schuster said:
Are you able to decode to a List? I don't have a Roc setup in front of me at the moment so trying to do this all in my head :sweat_smile:
I was not, I'm familiar enough with roc yet . This was my first mini project to see if I could convert one of my node scripts into roc
maybe this will help https://github.com/roc-lang/roc/blob/main/examples/python-interop/platform/main.roc
@Seth Workman I made an example in this zulip which has decoding json into a record.
It works, but it can be pretty fragile. There are at least a few decent bugs lurking in the Json builtin. I recently added the expect
s, so now I'm more confident I know how to test it, the next step is to find more of these bugs and iron them out. If you do find something that breaks, please log an issue that would be very helpful.
Yea I'm able to get basic json decoding working. But i can't get it working for my use case as my json doesn't have static key names
Note that URL json package is a little old now, so recommend using the one in main at roc/crates/compiler/builtins/roc
etc
Ah, I see...
I'm not sure if that is supported yet. I haven't spent enough time looking inside the Json module. I can have a look later this evening.
Alright no worries! Thanks for some insight
@Luke Boswell it should be possible to make the JSON format decode directly into a Dict
and have it Just Work as long as the Dict
keys and values both have Decode
decode directly into a Dict and have it Just Work
That's awesome!! Looking forward to testing it out.
but it will require adding Decode
to Dict
- it doesn't have it yet
oh that might be blocked on having the feature of "give this Decode
iff its k
and v
type variables also have Decode
" :thinking:
that feature already ezists
It will automatically be inferred as appropriate in either manual or derived implementations
Last updated: Jul 06 2025 at 12:14 UTC