beequeue/tweaky

A library for transforming JSON data

dev-master 2016-03-30 23:52 UTC

This package is not auto-updated.

Last update: 2024-04-27 17:03:36 UTC


README

Build Status Coverage Status

Tweaky is a library and domain-specific language in JSON notation allowing for the custom transformation of JSON payloads. It is more concerned with altering values than altering form. Primary use-case is for specifying modifications to API responses in a mocking proxy.

Usage

Include via composer:

composer require beequeue/tweaky

Example usage:

use Beequeue\Tweaky\Spec;
use Beequeue\Tweaky\Tweaky;

$inputJson =<<< END
{
  "a": 1,
  "b": "original",
  "c": [
    {"key": "first"},
    {"key": "second"},
    {"key": "last"}
  ],
  "d": "leave alone"
}
END;

$specJson =<<< END
{
  "transforms": [{
    "a": "{+10}",
    "b": "new value",
    "c": {
      "{[1]}": {
        "key": "middle"
      }
    }
  }]
}
END;

$spec = new Spec($specJson);
$tweaky = new Tweaky($spec);
$output = $tweaky->process($inputJson);

echo json_encode($output, JSON_PRETTY_PRINT);

will output:

{
    "a": 11,
    "b": "new value",
    "c": [
        {
            "key": "first"
        },
        {
            "key": "middle"
        },
        {
            "key": "last"
        }
    ],
    "d": "leave alone"
}