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

如何使用JSONata将源JSON中的键作为输出值?

动态键JSON转换为指定数组格式的解法

核心思路

遍历原JSON中skills对象的键值对,将每个键作为name字段的值,对应的值作为skills字段的值,最终把这些对象组合成数组即可。

JavaScript 实现

用Object.entries()获取键值对数组,再通过map()转换格式:

const sourceJson = {
  "skills":{
    "Axel":[
      "MSOffice",
      "SQL",
      "Photoshop"
    ],
    "Ronda":[
      "Googledocs",
      "VBA",
      "Figma"
    ]
  }
};

const result = Object.entries(sourceJson.skills).map(([name, skills]) => ({
  name,
  skills
}));

console.log(JSON.stringify(result, null, 2));

Python 实现

通过字典items()方法遍历键值对,生成目标列表:

import json

source_json = {
  "skills":{
    "Axel":[
      "MSOffice",
      "SQL",
      "Photoshop"
    ],
    "Ronda":[
      "Googledocs",
      "VBA",
      "Figma"
    ]
  }
}

result = [{"name": name, "skills": skills} for name, skills in source_json["skills"].items()]

print(json.dumps(result, indent=2))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 15:15:05