{"id":17635942,"url":"https://github.com/mohawk2/json-transform","last_synced_at":"2025-03-30T03:43:08.329Z","repository":{"id":56832492,"uuid":"119308138","full_name":"mohawk2/json-transform","owner":"mohawk2","description":"Arbitrary transformations of JSON-able data","archived":false,"fork":false,"pushed_at":"2020-01-01T15:20:45.000Z","size":101,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-05T06:14:22.685Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Perl","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mohawk2.png","metadata":{"files":{"readme":"README.md","changelog":"Changes","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-28T23:36:11.000Z","updated_at":"2020-01-01T15:20:41.000Z","dependencies_parsed_at":"2022-09-08T05:10:41.761Z","dependency_job_id":null,"html_url":"https://github.com/mohawk2/json-transform","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohawk2%2Fjson-transform","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohawk2%2Fjson-transform/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohawk2%2Fjson-transform/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohawk2%2Fjson-transform/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mohawk2","download_url":"https://codeload.github.com/mohawk2/json-transform/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246273515,"owners_count":20750904,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-23T02:24:44.076Z","updated_at":"2025-03-30T03:43:08.309Z","avatar_url":"https://github.com/mohawk2.png","language":"Perl","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NAME\n\nJSON::Transform - arbitrary transformation of JSON-able data\n\n# PROJECT STATUS\n\n| OS      |  Build status |\n|:-------:|--------------:|\n| Linux   | [![Build Status](https://travis-ci.org/mohawk2/json-transform.svg?branch=master)](https://travis-ci.org/mohawk2/json-transform) |\n\n[![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)\n\n# SYNOPSIS\n\n    use JSON::Transform qw(parse_transform);\n    use JSON::MaybeXS;\n    my $transformer = parse_transform(from_file($transformfile));\n    to_file($outputfile, encode_json $transformer-\u003e(decode_json $json_input));\n\n# DESCRIPTION\n\nImplements a language concisely describing a set of\ntransformations from an arbitrary JSON-able piece of data, to\nanother one. The description language uses [JSON Pointer (RFC\n6901)](https://tools.ietf.org/html/rfc6901) for addressing. JSON-able\nmeans only strings, booleans, nulls (Perl `undef`), numbers, array-refs,\nhash-refs, with no circular references.\n\nA transformation is made up of an output expression, which can be composed\nof sub-expressions. The general concept is of expressions, and\noperations. Operations are generally \"applied\" to expressions, with\nthe new value coming \"from\" the operation. That is why the `\u003c`\ncharacter is used for \"applying\" syntax.\n\nFor instance, to transform an array of hashes that each have an `id`\nkey, to a hash mapping each `id` to its hash:\n\n    # [ { \"id\": 1, \"name\": \"Alice\" }, { \"id\": 2, \"name\": \"Bob\" } ]\n    # -\u003e\n    \"\" \u003c@ { \"/$K/id\":$V#`id` }\n    # -\u003e\n    # { \"1\": { \"name\": \"Alice\" }, \"2\": { \"name\": \"Bob\" } }\n\nWhile to do the reverse transformation:\n\n    \"\" \u003c% [ $V@`id`:$K ]\n\nThe identity for an array:\n\n    \"\" \u003c@ [ $V ]\n\nThe identity for an object/hash:\n\n    \"\" \u003c% { $K:$V }\n\nTo get the keys of a hash:\n\n    \"\" \u003c% [ $K ]\n\nTo get how many keys in a hash:\n\n    \"\" \u003c% $C\n\nTo get how many items in an array:\n\n    \"\" \u003c@ $C\n\nTo move from one part of a structure to another:\n\n    \"/destination\" \u003c\u003c \"/source\"\n\nTo copy from one part of a structure to another:\n\n    \"/destination\" \u003c- \"/source\"\n\nTo do the same with a transformation (assumes `/source` is an array\nof hashes):\n\n    \"/destination\" \u003c- \"/source\" \u003c@ [ $V@`order`:$K ]\n\nTo bind a variable, then replace the whole data structure:\n\n    $defs \u003c- \"/definitions\"\n    \"\" \u003c- $defs\n\nA slightly complex transformation, using the [jt](https://metacpan.org/pod/jt) script:\n\n    $ cat \u003c\u003cEOF | jt '\"\" \u003c- \"/Time Series (Daily)\" \u003c% [ .{ `date`: $K, `close`: $V\u003c\"/4. close\" } ]'\n    {\n      \"Meta Data\": {},\n      \"Time Series (Daily)\": {\n        \"2018-10-26\": { \"1. open\": \"\", \"4. close\": \"106.9600\" },\n        \"2018-10-25\": { \"1. open\": \"\", \"4. close\": \"108.3000\" }\n      }\n    }\n    EOF\n    # produces:\n    [\n      {\"date\":\"2018-10-25\",\"close\":\"108.3000\"},\n      {\"date\":\"2018-10-26\",\"close\":\"106.9600\"}\n    ]\n\n## Expression types\n\n- Object/hash\n\n    These terms are used here interchangeably.\n\n- Array\n- String\n- Integer\n- Float\n- Boolean\n- Null\n\n## JSON pointers\n\nJSON pointers are surrounded by `\"\"`. JSON pointer syntax gives special\nmeaning to the `~` character, as well as to `/`. To quote a `~`,\nsay `~0`. To quote a `/`, say `~1`. Since a `$` has special meaning,\nto use a literal one, quote it with a preceding `\\`.\n\nThe output type of a JSON pointer is whatever the pointed-at value is.\n\n## Transformations\n\nA transformation has a destination, a transformation type operator, and\na source-value expression. The destination can be a variable to bind to,\nor a JSON pointer.\n\nIf the source-value expression has a JSON-pointer source, then the\ndestination can be omitted and the JSON-pointer source will be used.\n\nThe output type of the source-value expression can be anything.\n\n### Transformation operators\n\n- `\u003c-`\n\n    Copying (including assignment for variable bindings)\n\n- `\u003c\u003c`\n\n    Moving - error if the source-value is other than a bare JSON pointer\n\n## Destination value expressions\n\nThese can be either a variable, or a JSON pointer.\n\n### Variables\n\nThese are expressed as `$` followed by a lower-case letter, followed\nby zero or more letters.\n\n## Source value expressions\n\nThese can be either a single value including variables, of any type,\nor a mapping expression.\n\n## String value expressions\n\nString value expressions can be surrounded by ``` `` ```. They have the same\nquoting rules as in JSON's `\"`-surrounded strings, including quoting\nof `` ` `` using `\\`. Any value inside, including variables, will be\nconcatenated in the obvious way, and numbers will be coerced into strings\n(be careful of locale). Booleans and nulls will be stringified into\n`[true]`, `[false]`, `[null]`.\n\n## Literal arrays\n\nThese are a single value of type array, expressed as surrounded by `.[]`,\nwith zero or more comma-separated single values.\n\n## Literal objects/hashes\n\nThese are a single value of type object/hash, expressed as surrounded\nby `.{}`, with zero or more comma-separated colon pairs (see \"Mapping\nto an object/hash\", below).\n\n## Mapping expressions\n\nA mapping expression has a source-value, a mapping operator, and a\nmapping description.\n\nThe mapping operator is either `\u003c@`, requiring the source-value\nto be of type array, or `\u003c%`, requiring type object/hash. If the\ninput data pointed at by the source value expression is not the right\ntype, this is an error.\n\nThe mapping description must be surrounded by either `[]` meaning return\ntype array, or `{}` for object/hash.\n\nThe description will be evaluated once for each input value.\nWithin the brackets, `$K` and `$V` will have special meaning.\n\nFor an array input, each input will be each single array value, and `$K`\nwill be the zero-based array index.\n\nFor an object/hash input, each input will be each pair. `$K` will be\nthe object key being evaluated, of type string.\n\nIn either case, `$V` will be the relevant value, of whatever type from\nthe input. `$C` will be of type integer, being the number of inputs.\n\n### Mapping to an object/hash\n\nThe return value will be of type object/hash, composed of a set of pairs,\nexpressed within `{}` as:\n\n- a expression of type string\n- `:`\n- an expression of any type\n\n### Mapping to an array\n\nWithin `[]`, the value expression will be an arbitrary value expression.\n\n## Single-value modifiers\n\nA single value can have a modifier, followed by arguments.\n\n### `@`\n\nThe operand value must be of type object/hash.\nThe argument must be a pair of string-value, `:`, any-value.\nThe return value will be the object/hash with that additional key/value pair.\n\n### `#`\n\nThe operand value must be of type object/hash.\nThe argument must be a string-value.\nThe return value will be the object/hash without that key.\n\n### `\u003c`\n\nThe operand value must be of type object/hash or array.\nThe argument must be a JSON pointer.\nThe return value will be the value, but having had the JSON pointer applied.\n\n## Available system variables\n\n### `$K`\n\nAvailable in mapping expressions. For each data pair, set to either the\nzero-based index in an array, or the string key of an object/hash.\n\n### `$V`\n\nAvailable in mapping expressions. For each data pair, set to the value.\n\n### `$C`\n\nAvailable in mapping expressions. Set to the integer number of values.\n\n### `$E`\n\nSet to an object/hash which is the Perl `%ENV`, i.e. the process\nenvironment.\n\n## Comments\n\nAny `--` sequence up to the end of that line will be a comment,\nand ignored.\n\n# DEBUGGING\n\nTo debug, set environment variable `JSON_TRANSFORM_DEBUG` to a true value.\n\n# EXPORT\n\n## parse\\_transform\n\nOn error, throws an exception. On success, returns a function that can\nbe called with JSON-able data, that will either throw an exception or\nreturn the transformed data.\n\nTakes arguments:\n\n- $input\\_text\n\n    The text describing the transformation.\n\n# SEE ALSO\n\n[Pegex](https://metacpan.org/pod/Pegex)\n\n[RFC 6902 - JSON Patch](https://tools.ietf.org/html/rfc6902) - intended\nto change an existing structure, leaving it (largely) the same shape\n\n# AUTHOR\n\nEd J, `\u003cetj at cpan.org\u003e`\n\n# BUGS\n\nPlease report any bugs or feature requests on\n[https://github.com/mohawk2/json-transform/issues](https://github.com/mohawk2/json-transform/issues).\n\nOr, if you prefer email and/or RT: to `bug-json-transform\nat rt.cpan.org`, or through the web interface at\n[http://rt.cpan.org/NoAuth/ReportBug.html?Queue=JSON-Transform](http://rt.cpan.org/NoAuth/ReportBug.html?Queue=JSON-Transform). I will be\nnotified, and then you'll automatically be notified of progress on your\nbug as I make changes.\n\n# LICENSE AND COPYRIGHT\n\nCopyright 2018 Ed J.\n\nThis program is free software; you can redistribute it and/or modify it\nunder the terms of the the Artistic License (2.0). You may obtain a\ncopy of the full license at:\n\n[http://www.perlfoundation.org/artistic\\_license\\_2\\_0](http://www.perlfoundation.org/artistic_license_2_0)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohawk2%2Fjson-transform","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmohawk2%2Fjson-transform","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohawk2%2Fjson-transform/lists"}