You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过JOLT提取JSON数组值并关联id与date字段?

解决JOLT关联id、date与goals字段的问题

原始输入JSON

[
  {
    "id": 107110685,
    "rows": [
      {
        "date": "2022-07-05",
        "goals": []
      },
      {
        "date": "2022-07-06",
        "goals": []
      }
    ],
    "total": {
      "goals": []
    }
  },
  {
    "id": 107111205,
    "rows": [
      {
        "date": "2022-07-15",
        "goals": [
          {
            "conversion_type": "postclick",
            "cpa": "318.9"
          },
          {
            "conversion_type": "postview",
            "cpa": "79.73"
          }
        ]
      },
      {
        "date": "2022-07-16",
        "goals": [
          {
            "conversion_type": "postview",
            "cpa": "23.61"
          }
        ]
      }
    ],
    "total": {
      "goals": [
        {
          "conversion_type": "postview",
          "cpa": "42.77"
        },
        {
          "conversion_type": "postclick",
          "cpa": "363.55"
        }
      ]
    }
  }
]

当前失效的JOLT Spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "rows": {
          "*": {
            "goals": {
              "*": "[]"
            }
          }
        }
      }
    }
  }
]

修改后的JOLT Spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "id": "&1.rows.*.goals.*.id",
        "rows": {
          "*": {
            "date": "&2.&1.goals.*.date",
            "goals": {
              "*": {
                "*": "&3.&2.&1.&"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "rows": {
          "*": {
            "goals": {
              "*": "[]"
            }
          }
        }
      }
    }
  }
]

逻辑说明

  1. 第一个shift操作:把顶级对象的id和日期行的date,向下传递到每个goals对象的层级,和conversion_type、cpa字段合并
  2. 第二个shift操作:把所有带完整字段的goals元素,统一收集到一个顶级数组中
  3. 空的goals数组会自动被忽略,因为"*"匹配不到任何元素,正好符合需求

目标输出

[
  {
    "id": 107111205,
    "date": "2022-07-15",
    "conversion_type": "postclick",
    "cpa": "318.9"
  },
  {
    "id": 107111205,
    "date": "2022-07-15",
    "conversion_type": "postview",
    "cpa": "79.73"
  },
  {
    "id": 107111205,
    "date": "2022-07-16",
    "conversion_type": "postview",
    "cpa": "23.61"
  }
]

内容的提问来源于stack exchange,提问作者fujidaon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 12:18:27