How to use JSON in Raku

Google’s search results are missing an easy to find quickstart on how to deal with JSON in Raku (Perl 6), so I thought I would write a page for it.

First, install JSON::Tiny with the package manager:

$ zef install JSON::Tiny

Then in your script:

use JSON::Tiny;

say to-json({hello => 'world'});
# output: { "hello" : "world" }

say from-json('{"hello":"world"}');
# output: {hello => world}

To use it in a one-liner, load the module with -M first and use -ne:

$ curl -s | \
    raku -MJSON::Tiny -ne 'say from-json($_)<iss_position>'

It’s probably simpler to use jq for a one-liner like that, but I wanted to at least learn how to get it to run with Raku. Here’s the jq version:

$ curl -s | jq ".iss_position"

I hope this post saves someone 15 minutes. :slight_smile:


Related: How to make HTTP requests in Raku (Perl 6)