Skip to main content

transform

Function transform 

Source
pub fn transform(input: Value, spec: &TransformSpec) -> Result<Value>
Expand description

Perform JSON to JSON transformation where the “specification” is a JSON.

Inspired by Java library Jolt.

The transformation can compose of many operations that are chained together.

§Operations

  1. shift: copy data from the input tree and put it the output tree
  2. default: apply default values to the tree
  3. remove: remove data from the tree
  4. modify: modify data using functions like concat, toLower, etc.

For example, if you want to repack your JSON record, you can do the following:

use serde_json::{json, Value};
use harmony_jolt::{transform, TransformSpec};

let input: Value = serde_json::from_str(r#"
    {
        "id": 1,
        "name": "John Smith",
        "account": {
            "id": 1000,
            "type": "Checking"
        }
    }
"#).unwrap();

let spec: TransformSpec =
serde_json::from_str(r#"[
    {
      "operation": "shift",
      "spec": {
        "name": "data.name",
        "account": "data.account"
      }
    }
  ]"#).unwrap();

let output = transform(input, &spec).unwrap();

assert_eq!(output, json!({
    "data" : {
      "name": "John Smith",
      "account": {
        "id": 1000,
        "type": "Checking"
      }
    }
}));

Checkout supported operations in TransformSpec docs.