代码之家  ›  专栏  ›  技术社区  ›  Maxim Novakovski

过滤特定值/对象的JOLT转换

  •  0
  • Maxim Novakovski  · 技术社区  · 1 年前

    我想创建一个JOLT规范,过滤掉我需要的一切。

    我有这个JSON输入:

    {
      "StationDataList": {
        "StationData": {
          "ChannelData": [
            {
              "Values": {
                "VT": [
                  {
                    "t": "2023-12-13T00:15:00",
                    "content": -1
                  },
                  {
                    "t": "2023-12-13T00:30:00",
                    "content": -2
                  },
                  {
                    "t": "2023-12-13T00:45:00",
                    "content": -3
                  }
                ]
              },
              "channelId": "channelId1"
            },
            {
              "Values": {
                "VT": [
                  {
                    "t": "2023-12-13T01:00:00",
                    "content": 1
                  },
                  {
                    "t": "2023-12-13T02:00:00",
                    "content": 2.5
                  },
                  {
                    "t": "2023-12-13T03:00:00",
                    "content": 3
                  }
                ]
              },
              "channelId": "channelId2"
            }
          ],
          "timezone": "+01:00",
          "name": "stationName",
          "stationId": "123"
        }
      }
    }
    

    我想将每个VT对象提取到单独的对象中,并将字段stationId、channelId、名称和时区包括到每个对象中。

    我昨天试了一整天,有很多不同的规格,但没有一个能给我我需要的输出。 目前我有以下规格:

    [
      {
        "operation": "shift",
        "spec": {
          "StationDataList": {
            "StationData": {
              "ChannelData": {
                "*": {
                  "Values": {
                    "VT": {
                      "*": {
                        "@(5,stationId)": "[&4].stationId",
                        "@(5,timezone)": "[&4].timezone",
                        "@(5,name)": "[&4].name",
                        "@(3,channelId)": "[&4].channelId",
                        "t": "[&4].t",
                        "content": "[&4].content"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    ]
    

    它返回给我这个输出:

    [ {
      "stationId" : [ "123", "123", "123" ],
      "timezone" : [ "+01:00", "+01:00", "+01:00" ],
      "name" : [ "stationName", "stationName", "stationName" ],
      "channelId" : [ "channelId1", "channelId1", "channelId1" ],
      "t" : [ "2023-12-13T00:15:00", "2023-12-13T00:30:00", "2023-12-13T00:45:00" ],
      "content" : [ -1, -2, -3 ]
    }, {
      "stationId" : [ "123", "123", "123" ],
      "timezone" : [ "+01:00", "+01:00", "+01:00" ],
      "name" : [ "stationName", "stationName", "stationName" ],
      "channelId" : [ "channelId2", "channelId2", "channelId2" ],
      "t" : [ "2023-12-13T01:00:00", "2023-12-13T02:00:00", "2023-12-13T03:00:00" ],
      "content" : [ 1, 2.5, 3 ]
    } ]
    
    

    但我希望它们都是独立的对象,而不是像这样都在一个数组中:

    {
      "stationId" : "123",
      "timezone" : "+01:00",
      "name" : "stationName",
      "channelId" : "channelId1",
      "t" : "2023-12-13T00:15:00",
      "content" : -1
    },
    {
      "stationId" : "123",
      "timezone" : "+01:00",
      "name" : "stationName",
      "channelId" : "channelId1",
      "t" : "2023-12-13T00:30:00",
      "content" : -2
    }
    ...
    

    我怎样才能做到这一点?为什么他把所有东西都添加到一个数组中,而不是创建单独的对象?如果有任何帮助,我将不胜感激。

    1 回复  |  直到 1 年前
        1
  •  0
  •   Barbaros Özhan    1 年前

    由于JSON格式的限制,没有数组包装器的独立对象是不可能的,但如果需要将输出作为对象数组,则应通过两个数组的索引来分隔对象层: ChannelData VT 例如

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "*": {
                    "VT": {
                      "*": {
                        "@5,stationId": "&4_&1.stationId",
                        "@5,timezone": "&4_&1.timezone",
                        "@5,name": "&4_&1.name",
                        "@3,channelId": "&4_&1.channelId",
                        "*": "&4_&1.&"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      { // get rid of the individual object keys 
        // while wrapping whole JSON with square brackets
        "operation": "shift",
        "spec": {
          "*": "[]"
        }
      }
    ]
    
    推荐文章