Skip to content

Provides functions to select parts of a JSON-like structure

Notifications You must be signed in to change notification settings

raku-community-modules/JSONMask

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

JSON::Mask

Allows to filter JSON-like data so it's suitable for public consumption. The pattern describes the schema, and the module trims the extra data.

Basic Syntax

To select keys, list them in a comma-separated string:

# Select keys `a`, `b`, and `c`.
mask('a,b,c', %data);

To select all keys except a few, negate them:

# Select all the keys that aren't `a` or `b`.
mask('-a,-b', %data);

To select subkeys, use parentheses:

# Keeps only `a`, and in it only its subkeys `b` and `c`.
mask('a(b,c)', %data);

You can of course combine them:

# Select everything but `password`, but only keep the `name` and `email` subkeys from `profile`.
mask('-password,profile(name,email)', %data);

You can quote a key if it contains "special" characters:

# Select everything but `password` and `password-confirmation`.
mask('-password,-"password-confirmation"', %data);

Compilation

If you want to reuse masks, you can pre-compile them:

my $mask = compile-mask('a,b,c');
mask($mask, %data1);
mask($mask, %data2);
mask($mask, %data3);

Array handling

The module handles arrays without you needing to do anything -- a mask will be applied recursively on each element of the array.

my $data =
    %(id => 1, name => "First Volume"),
    %(id => 2, name => "Second adventure"),
    %(id => 3, name => "Final Countdown")
  ],
mask('id', $data); # Select key `a` in each sub-hash

Error handling

The module ignores missing keys. It will however throw an exception if a nested key (a(b,c)) is not actually Associative (or Positional).

About

Provides functions to select parts of a JSON-like structure

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages