jsonjolt

Jolt spec to lookup keys of an array


I am trying to replace values of an array based on a lookup map using Jolt, but the spec does not seem to work. It works till reaching the lookup map and I can shift the entire map, but referring to each key using & is not working as expected.

Input:

{
  "records": [
    {
      "id": "1",
      "keys": [
        "ba2c89c3",
        "aa2c89c3"
      ]
    }
  ],
  "lookup": {
    "ba2c89c3": "EE",
    "aa2c89c3": "EL"
  }
}

Expected Output:

{
  "data": {
    "records": [
      {
        "id": "1",
        "vals": [
          "EE",
          "EL"
        ]
      }
    ]
  }
}

Spec I tried but doesn't seem to work.

[
  {
    "operation": "shift",
    "spec": {
      "records": {
        "*": {
          "id": "data.records[&1].id",
          "keys": {
            "*": {
              "@(4,lookup.&)": "data.records[&3].vals"
            }
          }
        }
      }
    }
  }
]


Solution

  • You can match by the following shift transformation spec in which the innermost "&" on the left hand side will do the trick :

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "id": "data.&2[&1].&",//&2[&1] represents "records" array along with subindexes  
              "keys": {
                "*": {
                  "*": {
                    "@5,lookup": { //need to go 5 levels up to be able to the whole nesting
                      "&": "data.&6[&5].vals[]"//& on the left side provides the equality between the values  
                    }
                  }
                }
              }
            }
          }
        }
      }
    ]
    

    where vals array will completely disappear if none of the values match

    the demo on the site Jolt Transform Demo Using v0.1.1 is :

    Jolt Transform Demo