Home > Enterprise >  Jolt Transformation - pull up and rename field
Jolt Transformation - pull up and rename field

Time:12-20

I am trying to write a jolt transformation with the below input:

{
  "data": {
    "positions": {
      "positionEdge": [
        {
          "position": {
            "ref": "B125AE024:1:BASE",
            "catalogue": {
              "ref": "BASE:1"
            }
          },
          "cursor": "Y3Vyc29yOi0tLWMxMWYxYWQwLTE2MWEtNDNmNS05ZDM5LWMwODRiZTdiN2Q3OV9fMTY1NzQ5NTU5MTQ4Ng=="
        },
        {
          "position": {
            "ref": "B125AE024:2:AGGREGATE",
            "catalogue": {
              "ref": "ATS:1"
            }
          },
          "cursor": "Y3Vyc29yOi0tLWVmZDgwNTljLWYyNTctNDhhYy1hYzVlLWI3NzlhMjMyMTVmYl9fMTY1NzQ5NTU5MTI3MQ=="
        }
      ],
      "pageInfo": {
        "hasNextPage": true
      }
    }
  }
}

The expected output is:

[
  {
    "ref": "B125AE024:1:BASE",
    "catalogueRef": "BASE:1"
  },
  {
    "ref": "B125AE024:2:AGGREGATE",
    "catalogueRef": "ATS:1"
  }
]

My current spec is:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "positions": {
          "positionEdge": {
            "*": {
              "position": {
                "@": ".",
                "catalogue": {
                  "ref": "catalogueRef"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "catalogue": ""
      }
    }
  }
]

which does not give me the desired result and is missing the catalogueRef of the 2nd record..

Can the result be achieved in a single transformation, i.e. rename the catalogue.ref field up and rename it to catalogueRef? I basically want to flatten the position records.

Your help would be much appreciated.

CodePudding user response:

Yes, you can use a single shift transformation spec, by walking through the "positionEdge" array, in a symbolical manner such as

[
  {
    "operation": "shift",
    "spec": {
      "*": { // stands for the level of the object "data"
        "*": { // the level of the object "positions"
          "*": { // the level of the array "positionEdge"
            "*": { //the indexes of the array "positionEdge"
              "position": { // the level of the object "position" 
                "*": "[#3].&", // going three levels up the tree to reach the level of "positionEdge" by three times traversing the "{" sign, and replicate "ref" attribute 
                "c*": { //the level of object "catalogue" -- else case
                  "*": "[#4].&1&" // going four levels up the tree to reach the level of "positionEdge", &1 brings the literal "catalogue", and & already replicates the level from the current level, eg. "ref"
                }
              }
            }
          }
        }
      }
    }
  }
]

the demo screenshot on the site enter image description here

If the capital letter R is really needed, then you can convert the leaf node from

"*": "[#4].&1&"

to

"r*": "[#4].&1R&(0,1)" // where, in &(0,1); zero represents the current level, one represents the 1st occurence for the asterisk, which might be more than one, in order to generate the literal "ef"

CodePudding user response:

Here's the spec to get the desired output

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "positions": {
          "positionEdge": {
            "*": {
              "position": {
                "ref": "[&2].ref",
                "catalogue": {
                  "ref": "[&3].catalogueRef"
                }
              }
            }
          }
        }
      }
    }
  }
]

the demo on the site enter image description here

  • Related