Stream: beginners

Topic: Json Package


view this post on Zulip Luke Boswell (Feb 27 2023 at 07:09):

I've been experimenting with URL packages and made this example using Json which I think is pretty neat. If anyone runs this please lets me know; I'm interested know if the Json URL pacakge works for you!

Try running both roc run and roc test... just copy and paste below into a .roc file and it should just work :fingers_crossed:

app "example"
    packages {
        cli: "https://github.com/roc-lang/basic-cli/releases/download/0.2.1/wx1N6qhU3kKva-4YqsVJde3fho34NqiLD3m620zZ-OI.tar.br",
        json: "https://asdfasdf23452.s3.ap-southeast-2.amazonaws.com/cdKMia6cwdRG6Gb0SfXP8cgGF7yTz-i959FV6ZfuS0E.tar.br",
    }
    imports [
        cli.Stdout,
        json.Core.{ Json, toUtf8, fromUtf8 },
        Decode,
    ]
    provides [main] to cli

main = Stdout.line "Hello \(person.name)!"

Person : {
    name : Str,
}

inputStr : Str
inputStr = "{\"name\":\"John Smith\"}"

person : Person
person =
    inputStr
    |> Str.toUtf8
    |> Decode.fromBytes fromUtf8
    |> Result.onErr \_ -> crash "bad decode"
    |> Result.withDefault { name: "" }

outputStr : Str
outputStr =
    person
    |> Encode.toBytes toUtf8
    |> Str.fromUtf8
    |> Result.onErr \_ -> crash "bad encode"
    |> Result.withDefault ""

expect outputStr == inputStr
luke@192-168-1-104 examples % roc run hello-world.roc
Hello John Smith!
luke@192-168-1-104 examples % roc test hello-world.roc

0 failed and 1 passed in 637 ms.

view this post on Zulip Anton (Feb 27 2023 at 10:56):

Everything worked as expected on Ubuntu 20.04 :)

view this post on Zulip Richard Feldman (Feb 27 2023 at 13:52):

worked for me on macOS Monterey 12.6.3!


Last updated: Jul 06 2025 at 12:14 UTC