我遇到了一个问题,使用jolt转换转换一个非常复杂的嵌套JSON.输入和输出细节如下所示.

我试图创建一个震动规格,但没有得到它.有人可以请帮助我.详情如下:

输入JSON

{
  "key": [
    {
      "date": "27/09/2023"
    }
  ],
  "value": [
    {
      "values": [
        {
          "location": "3005",
          "total_capacity": 24,
          "capacity_flag": "",
          "orders": [
            {
              "order_type": "INIT",
              "consumed_capacity": 0
            },
            {
              "order_type": "PUSH",
              "consumed_capacity": 0
            }
          ]
        },
        {
          "location": "3007",
          "total_capacity": 72,
          "capacity_flag": "",
          "orders": [
            {
              "order_type": "INIT",
              "consumed_capacity": 0
            },
            {
              "order_type": "PUSH",
              "consumed_capacity": 0
            },
            {
              "order_type": "RPLN",
              "consumed_capacity": 0
            }
          ]
        }
      ]
    }
  ]
}

期望输出

[
  {
    "date": "27/09/2023",
    "location": "3005",
    "total_capacity": 24,
    "capacity_flag": "",
    "order_type": "INIT",
    "consumed_capacity": 0
  },
  {
    "date": "27/09/2023",
    "location": "3005",
    "total_capacity": 24,
    "capacity_flag": "",
    "order_type": "PUSH",
    "consumed_capacity": 0
  },
  {
    "date": "27/09/2023",
    "location": "3007",
    "total_capacity": 72,
    "capacity_flag": "",
    "order_type": "INIT",
    "consumed_capacity": 0
  },
  {
    "date": "27/09/2023",
    "location": "3007",
    "total_capacity": 72,
    "capacity_flag": "",
    "order_type": "PUSH",
    "consumed_capacity": 0
  },
  {
    "date": "27/09/2023",
    "location": "3007",
    "total_capacity": 72,
    "capacity_flag": "",
    "order_type": "RPLN",
    "consumed_capacity": 0
  }
]

我试过的Jolt Spec,但日期不正确

[
  {
    "operation": "shift",
    "spec": {
      "value": {
        "*": {
          "values": {
            "*": {
              "orders": {
                "*": {
                  "order_type": "&3.&1.order_type",
                  "consumed_capacity": "&3.&1.consumed_capacity",
                  "@2,location": "&3.&1.location",
                  "@2,total_capacity": "&3.&1.total_capacity",
                  "@2,capacity_flag": "&3.&1.capacity_flag"
                }
              }
            }
          }
        }
      }
    }
  },
  { // get rid of the object keys generated within the previous spec
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

需要帮助写一个震动规范,使我们得到一个扁平数组

推荐答案

您只需再次获取日期,然后再将其包含到您的输出中.

以下是你如何做到这一点的 idea (请注意,我假设你永远只有一次约会)

[
  {
    "operation": "shift",
    "spec": {
      "value": {
        "*": {
          "values": {
            "*": {
              "orders": {
                "*": {
                  "@(6,key[0].date)": "&3.&1.date",
                  "order_type": "&3.&1.order_type",
                  "consumed_capacity": "&3.&1.consumed_capacity",
                  "@2,location": "&3.&1.location",
                  "@2,total_capacity": "&3.&1.total_capacity",
                  "@2,capacity_flag": "&3.&1.capacity_flag"
                }
              }
            }
          }
        }
      }
    }
  },
  { // get rid of the object keys generated within the previous spec
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

Json相关问答推荐

如何编写MongoDB查询以返回数组数组

在Zig中解析JSON失败

如何在JSONata对象中迭代并向数组添加新字段?

在 python 中循环 JSON 数组

迭代powershell双维json对象

在 postgres 14 中将记录转换为所需的 json 格式

Powershell解析JSON文件中的键或值

使用 jq 从字符串列表开始创建对象

为什么解析的字典相等而腌制的字典不相等?

如果 JSON 对象包含列表中的子字符串,则丢弃它们

PowerShell - JSON/PsCustomObject - 为什么我的数组被扁平化为一个对象?

Play Framework:如何序列化/反序列化与 JSON 的枚举

一起使用 Argparse 和 Json

Jackson 中的 readValue 和 readTree:何时使用哪个?

Json.Net:用于自定义命名的 JsonSerializer-Attribute

Jsonpath 与 Jackson 或 Gson

有什么方法可以在 elasticsearch 服务器中导入 json 文件(包含 100 个文档).?

如何在 Java 中将 YAML 转换为 JSON?

将多个值存储在json中的单个键中

Backbone.js 模型与集合