通过 JOLT 变换对数据进行分组

问题描述 投票:0回答:1

我是 JOLT 语言的新手,对转换有点头疼。我的 JSON 输入是这个

[
  {
    "table": 0,
    "_time": "2024-05-08T10:33:48.14Z",
    "_value": 40976,
    "parameter": "A",
    "station": "ABC",
    "stationName": "AABBCC"
  },
  {
    "table": 0,
    "_time": "2024-05-08T10:34:33.372Z",
    "_value": 42009,
    "parameter": "A",
    "station": "ABC",
    "stationName": "AABBCC"
  },
  {
    "table": 1,
    "_time": "2024-05-08T10:33:48.141Z",
    "_value": 22091,
    "parameter": "B",
    "station": "XYZ",
    "stationName": "XXYYZZ"
  },
  {
    "table": 1,
    "_time": "2024-05-08T10:34:10.231Z",
    "_value": 21968,
    "parameter": "B",
    "station": "XYZ",
    "stationName": "XXYYZZ"
  }
]

我期待着这个:

[
  {
    "stationName": "AABBCC",
    "content": [
      {
        "table": 0,
        "_time": "2024-05-08T10:33:48.14Z",
        "_value": 40976,
        "parameter": "A",
        "station": "ABC"
      },
      {
        "table": 0,
        "_time": "2024-05-08T10:34:33.372Z",
        "_value": 42009,
        "parameter": "A",
        "station": "ABC"
      }
    ]
  },
  {
    "stationName": "XXYYZZ",
    "content": [
      {
        "table": 1,
        "_time": "2024-05-08T10:33:48.141Z",
        "_value": 22091,
        "parameter": "B",
        "station": "XYZ"
      },
      {
        "table": 1,
        "_time": "2024-05-08T10:34:10.231Z",
        "_value": 21968,
        "parameter": "B",
        "station": "XYZ"
      }
    ]
  }
]

如您所见,我需要以下内容:

  • 基于“station”,对不同数组中的数据进行分组
  • 将“stationName”向上移动一级,移出数组
  • 所有 JSON 都需要是一个数组

我能走的最远就是这个规格

[
  {
    "operation": "shift",
    "spec": {
      "*": "@station"
    }
    },
  {
    "operation": "shift",
    "spec": {
      "@": "[]"
    }
  }
]

这给了我这个:

[
  {
    "ABC": [
      {
        "table": 0,
        "_time": "2024-05-08T10:33:48.14Z",
        "_value": 40976,
        "parameter": "A",
        "station": "ABC",
        "stationName": "AABBCC"
      },
      {
        "table": 0,
        "_time": "2024-05-08T10:34:33.372Z",
        "_value": 42009,
        "parameter": "A",
        "station": "ABC",
        "stationName": "AABBCC"
      }
    ],
    "XYZ": [
      {
        "table": 1,
        "_time": "2024-05-08T10:33:48.141Z",
        "_value": 22091,
        "parameter": "B",
        "station": "XYZ",
        "stationName": "XXYYZZ"
      },
      {
        "table": 1,
        "_time": "2024-05-08T10:34:10.231Z",
        "_value": 21968,
        "parameter": "B",
        "station": "XYZ",
        "stationName": "XXYYZZ"
      }
    ]
  }
]

说实话,我不知道我正在寻找的东西是否可能。重要的是要澄清我需要概括这个表达,因为“车站”并不总是相同的。

至少。我会接受这个:

[
  {
    "stationName": "XXYYZZ",
    "ABC": [
      {
        "table": 0,
        "_time": "2024-05-08T10:33:48.14Z",
        "_value": 40976,
        "parameter": "A",
        "station": "ABC"
      },
      {
        "table": 0,
        "_time": "2024-05-08T10:34:33.372Z",
        "_value": 42009,
        "parameter": "A",
        "station": "ABC"
      }
    ],
    "XYZ": [
      {
        "table": 1,
        "_time": "2024-05-08T10:33:48.141Z",
        "_value": 22091,
        "parameter": "B",
        "station": "XYZ"
      },
      {
        "table": 1,
        "_time": "2024-05-08T10:34:10.231Z",
        "_value": 21968,
        "parameter": "B",
        "station": "XYZ"
      }
    ]
  }
]

但我真的更喜欢前一个

非常感谢!

arrays json transformation jolt
1个回答
0
投票

你的方法很好,但是需要把

stationName
拿出来,而不是全部嵌套,所以我们应该单独操作
stationName
属性。 RHS 上的
@1,stationName
在此操作期间将很有用,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "stationName": "@1,stationName.&", //separate the stationName vs. others
        "*": "@1,stationName.content.&1.&"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "stationName": {
          "0": "&2.&1" //pick only one componet from the array 
        },
        "content": {
          "*": {
            "*": "&3.&2[#2].&" //&2 means grabbing the lieral "content" after going 2 levels up the tree 
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "[]"
    }
  }
]

网站上的 演示 https://jolt-demo.appspot.com/ 是:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.