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
- Host: GitHub
- URL: https://github.com/mohawk2/json-transform
- Owner: mohawk2
- Created: 2018-01-28T23:36:11.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-01-01T15:20:45.000Z (over 6 years ago)
- Last Synced: 2025-02-05T06:14:22.685Z (over 1 year ago)
- Language: Perl
- Size: 98.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: Changes
Awesome Lists containing this project
README
# NAME
JSON::Transform - arbitrary transformation of JSON-able data
# PROJECT STATUS
| OS | Build status |
|:-------:|--------------:|
| Linux | [](https://travis-ci.org/mohawk2/json-transform) |
[](https://metacpan.org/pod/JSON::Transform) [](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)