我想创建一个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、name和timezone字段.

我昨天试了一整天,有很多不同的规格,但没有一个给我所需要的输出. 目前我有这个规格:

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

我怎么能做到呢?为什么他要把所有东西都添加到数组中,而不是创建单独的对象?我会很感激你的帮助.

推荐答案

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

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

事实上,还有另一种方法,其中不需要单独编写每个属性:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": {
                "VT": {
                  "*": {
                    "@3|@5": {
                      "*": "&5_&2.&",
                      "&3|&5": "remElements" // determine the wrapper objects/arrays
                                             // to be deleted to keep only 
                                             // the attributes from those levels
                    },
                    "*": "&4_&1.&" // 4 and 1 represent the levels of the indexes 
                                   // of each arrays respectively
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "remElements": { "": "" }, // get rid of wrapper elements
      "*": "[]" // nest remaining attributes within an array of objects
    }
  }
]

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

enter image description here

Json相关问答推荐

从Razor Pages的AJAX Json呈现DataTables问题.Net GET

我发现GoFr响应总是包含在数据字段中,如何返回自定义响应?

织女星-没有循环的动画条形图第二部分(实际上是织女星)

将pyspark.sql.Rowtype数据转换为Json字符串,消除Azure Databricks NB中的值

导致此 Kotlin Retrofit2 错误的可能原因有哪些?

GitHub Pages无法正确显示我的项目

如何在 Apache NiFi 中使用 JoltTransformJson 删除流文件或具有空字段的整个对象?

将具有多个级别的 json 读入 DataFrame [python]

使用 KQL 和外部 data() 运算符从 json 文件中提取信息

为什么在我们有 json_encode 时使用 CJSON 编码

可以通过 POST 使用 EventSource 传递参数的服务器发送事件 (SSE)

在 JSON 反序列化期间没有为System.String类型定义无参数构造函数

使用 jq,将对象数组转换为具有命名键的对象

在 JSON 编码的 HTML5 数据属性中转义/编码单引号

使用 GSON 解析嵌套的 JSON 数据

Spring MVC:不反序列化 JSON 请求正文

jQuery循环.each()JSON键/值不起作用

[__NSCFNumber 长度]:发送到实例 UITableView 的无法识别的 Select 器

可以在 SharedPreferences 中保存 JSON 数组吗?

如何从 BindingResult 获取控制器中的错误文本