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

如何使用JOLT提取键的列值并转换嵌套JSON为SQL适配格式

将嵌套JSON转换为适配SQL预处理的数组格式

要把你提供的嵌套JSON转换成适合SQL预处理的扁平数组格式,可通过遍历原JSON的键值对,提取产品名称并与对应统计字段合并来实现。以下是两种常用编程语言的实现方案:

Python 实现

import json

# 输入JSON字符串
input_json = '''{
  "Product1": {
    "Purchase": 31
  },
  "Product2": {
    "Purchase": 6213,
    "Cancel": 1988,
    "Change": 3702,
    "Renewal": 5934
  }
}'''

# 解析JSON数据
data = json.loads(input_json)

# 转换格式
result = []
for product_name, stats in data.items():
    # 创建新对象,合并产品名和统计字段
    item = {"product": product_name}
    item.update(stats)
    result.append(item)

# 输出格式化后的JSON
print(json.dumps(result, indent=2))

JavaScript 实现

// 输入的JSON对象
const inputData = {
  "Product1": {
    "Purchase": 31
  },
  "Product2": {
    "Purchase": 6213,
    "Cancel": 1988,
    "Change": 3702,
    "Renewal": 5934
  }
};

// 转换格式:遍历键值对,合并成新对象
const result = Object.entries(inputData).map(([productName, stats]) => ({
  product: productName,
  ...stats
}));

// 输出格式化后的结果
console.log(JSON.stringify(result, null, 2));

说明

转换后的数组结构中,每个对象对应数据库表的一行记录,product字段作为产品标识,其余字段对应各类操作的统计值,这种格式可直接用于SQL批量插入或数据导入操作,适配大多数数据库的预处理需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 08:01:01