An open API service indexing awesome lists of open source software.

https://github.com/mohawk2/json-transform

Arbitrary transformations of JSON-able data
https://github.com/mohawk2/json-transform

Last synced: about 1 year ago
JSON representation

Arbitrary transformations of JSON-able data

Awesome Lists containing this project

README

          

# NAME

JSON::Transform - arbitrary transformation of JSON-able data

# PROJECT STATUS

| OS | Build status |
|:-------:|--------------:|
| Linux | [![Build Status](https://travis-ci.org/mohawk2/json-transform.svg?branch=master)](https://travis-ci.org/mohawk2/json-transform) |

[![CPAN version](https://badge.fury.io/pl/JSON-Transform.svg)](https://metacpan.org/pod/JSON::Transform) [![Coverage Status](https://coveralls.io/repos/github/mohawk2/json-transform/badge.svg?branch=master)](https://coveralls.io/github/mohawk2/json-transform?branch=master)

# SYNOPSIS

use JSON::Transform qw(parse_transform);
use JSON::MaybeXS;
my $transformer = parse_transform(from_file($transformfile));
to_file($outputfile, encode_json $transformer->(decode_json $json_input));

# DESCRIPTION

Implements a language concisely describing a set of
transformations from an arbitrary JSON-able piece of data, to
another one. The description language uses [JSON Pointer (RFC
6901)](https://tools.ietf.org/html/rfc6901) for addressing. JSON-able
means only strings, booleans, nulls (Perl `undef`), numbers, array-refs,
hash-refs, with no circular references.

A transformation is made up of an output expression, which can be composed
of sub-expressions. The general concept is of expressions, and
operations. Operations are generally "applied" to expressions, with
the new value coming "from" the operation. That is why the `<`
character is used for "applying" syntax.

For instance, to transform an array of hashes that each have an `id`
key, to a hash mapping each `id` to its hash:

# [ { "id": 1, "name": "Alice" }, { "id": 2, "name": "Bob" } ]
# ->
"" <@ { "/$K/id":$V#`id` }
# ->
# { "1": { "name": "Alice" }, "2": { "name": "Bob" } }

While to do the reverse transformation:

"" <% [ $V@`id`:$K ]

The identity for an array:

"" <@ [ $V ]

The identity for an object/hash:

"" <% { $K:$V }

To get the keys of a hash:

"" <% [ $K ]

To get how many keys in a hash:

"" <% $C

To get how many items in an array:

"" <@ $C

To move from one part of a structure to another:

"/destination" << "/source"

To copy from one part of a structure to another:

"/destination" <- "/source"

To do the same with a transformation (assumes `/source` is an array
of hashes):

"/destination" <- "/source" <@ [ $V@`order`:$K ]

To bind a variable, then replace the whole data structure:

$defs <- "/definitions"
"" <- $defs

A slightly complex transformation, using the [jt](https://metacpan.org/pod/jt) script:

$ cat <`

# BUGS

Please report any bugs or feature requests on
[https://github.com/mohawk2/json-transform/issues](https://github.com/mohawk2/json-transform/issues).

Or, if you prefer email and/or RT: to `bug-json-transform
at rt.cpan.org`, or through the web interface at
[http://rt.cpan.org/NoAuth/ReportBug.html?Queue=JSON-Transform](http://rt.cpan.org/NoAuth/ReportBug.html?Queue=JSON-Transform). I will be
notified, and then you'll automatically be notified of progress on your
bug as I make changes.

# LICENSE AND COPYRIGHT

Copyright 2018 Ed J.

This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:

[http://www.perlfoundation.org/artistic\_license\_2\_0](http://www.perlfoundation.org/artistic_license_2_0)