简体   繁体   中英

Jolt Transformation - pull up and rename field

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, ie 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.

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 http://jolt-demo.appspot.com/ is

在此处输入图像描述

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 http://jolt-demo.appspot.com/ ( the famous sandbox of the jolt ) is:

在此处输入图像描述

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM