json - Jolt 将值从数组移动到顶层

标签 json jolt

我有一个像这样的 JSON:

[
  {
    "id": 1015380,
    "type": "campaign",
    "stats": [
      {
        "month": "2023-07",
        "spent": "3116.90",
        "impressions": 17783,
        "clicks": 78
      },
      {
        "month": "2023-08",
        "spent": "1490.36",
        "impressions": 6721,
        "clicks": 17
      }
    ]
  },
  {
    "id": 1015695,
    "type": "campaign",
    "stats": [
      {
        "month": "2023-08",
        "spent": "1490.36"
      }
    ]
  }
]

我想将 stats 数组取消嵌套到顶层并获得如下结果:

enter image description here

尝试使用下面的脚本,但它将来自 stats 的值存储为数组:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "stats": {
          "*": {
            "@(2,id)": "[&3].id",
            "*": "[&3].&"
          }
        }
      }
    }
  }
]

如何解决?

最佳答案

您可以使用以下shift转换规范

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "stats": {
          "*": {
            "@2,id": "&3_&1.id",
            "*": "&3_&1.&"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

在当前情况下,仅遍历最外层对象的索引值,而此建议情况将遍历统计数组的索引(例如&1)以及分组依据两个不同的标识符在数组中生成独立的对象

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

enter image description here

关于json - Jolt 将值从数组移动到顶层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76960072/

相关文章:

php - Json数组数据在php中插入空值

java - JOLT - 根据 if 条件在 JSONArray 中添加字段

java - 尽管我们的基准程序每秒创建数百万个对象,但日志中报告的垃圾收集很少

json - 如何使用jolt将嵌套数据变成线性数据

json - JOLT 转换将元素添加到数组

c++ - 如何使用curl准备http缓冲区

java - 我如何解析java中包含斜杠的json对象

python - Blender 2.6 JSON 导出器,纹理仅在立方体的一侧错误

javascript - JSON 模式解析器 Javascript

java - 数组输入的 Jolt 变换 JSON 规范