json Jolt转换-上拉并重命名字段

o3imoua4  于 2022-12-24  发布在  其他
关注(0)|答案(2)|浏览(147)

我尝试用下面的输入编写一个震动变换:

{
  "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
      }
    }
  }
}

预期输出为:

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

我的当前规格为:

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

这没有给予我想要的结果,并且缺少第2条记录的catalogueRef。
结果可以在一次转换中实现吗,即将catalogue.ref字段重命名为catalogueRef,我基本上想将位置记录扁平化。
你的帮助将不胜感激。

6ie5vjzr

6ie5vjzr1#

下面是获得所需输出的规范

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

站点http://jolt-demo.appspot.com/上的演示是

vsaztqbk

vsaztqbk2#

是的,您可以使用单个shift转换规范,方法是以符号方式遍历**"positionEdge"**数组,例如

[
  {
    "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"
                }
              }
            }
          }
        }
      }
    }
  }
]

网站http://jolt-demo.appspot.com/上的**演示截图***( 著名的jolt沙箱 *)是:

如果确实需要大写字母R,则可以将叶节点从

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

"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"

相关问题