简体   繁体   English

JOLT 转换 - 枢轴

[英]JOLT Transformation - Pivot

I'm trying to transform the following JSON我正在尝试转换以下 JSON

[
  {
    "name": "Buy",
    "List": [
      {
        "x": "7/8/2021",
        "y": 462853
      },
      {
        "x": "7/9/2021",
        "y": 462777
      },
      {
        "x": "7/10/2021",
        "y": 462701
      }
    ]
  },
  {
    "name": "Statistical",
    "List": [
      {
        "x": "7/8/2021",
        "y": 462853
      },
      {
        "x": "7/9/2021",
        "y": 462777
      },
      {
        "x": "7/10/2021",
        "y": 462701
      }
    ]
  },
  {
    "name": "Sell",
    "List": [
      {
        "x": "7/8/2021",
        "y": 462853
      },
      {
        "x": "7/9/2021",
        "y": 462777
      },
      {
        "x": "7/10/2021",
        "y": 462701
      }
    ]
  }
]

Into something simpler like this, using JOLT:像这样更简单的东西,使用 JOLT:

[
  {
     "Date": "7/8/2021",
     "Buy": 462853,
     "Statistical": 462853,
     "Sell": 462853
  },
  {
     "Date": "7/9/2021",
     "Buy": 462777,
     "Statistical": 462777,
     "Sell": 462777
  },
  {
     "Date": "7/10/2021",
     "Buy": 462701,
     "Statistical": 462701,
     "Sell": 462701
  }
]

I try a lot of jolt code but I can't figure out how to do the last part.我尝试了很多 jolt 代码,但我不知道如何做最后一部分。 I wrote some jolt transformation like this:我写了一些这样的颠簸转换:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "List": {
          "*": {
            "@(2,name)": "[&(3)].&1.name",
            "x": "[&(3)].&1.Date",
            "y": "[&(3)].&1.NAV"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[]"
      }
    }
  }
]

But I don't know how to do the final part to pivot the name column.但我不知道如何做最后一部分来旋转名称列​​。 The value of the name column should pivot based on NAV value. name 列的值应根据 NAV 值进行透视。

You can accumulate the key-value pairs of name keys by x values as represented by Date key within the shift transformation, and then pick leftmost one of the elements of each arrays those already have repeatedly same elements throughout within each by using cardinality transformation such as您可以通过移位转换中的Date键表示的x值累积name键的键值对,然后通过使用基数转换从每个数组中选择最左边的一个元素,这些元素在每个数组中已经重复相同的元素,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "List": {
          "*": {
            "x": "[&1].Date",
            "y": "[&1].@(3,name)"
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "Date": "ONE"
      }
    }
  }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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