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

如何从项目JSON文件提取id与traits字段生成新JSON文件?

问题:提取JSON数组中的指定字段生成新JSON文件

我有一个源自项目数据的JSON相关数据(以JavaScript变量形式存储),其中包含冗余信息,仅需要保留每个对象的id和traits字段,如何提取这两个字段并生成新的JSON文件?

原数据如下:

const toSql = [
    {"id":1,"battlerName":"Actor1_1","characterIndex":0,"characterName":"Actor1","classId":1,"equips":[2,0,0,3,0],"faceIndex":0,"faceName":"Actor1","traits":[{"code":51,"dataId":2,"value":1},{"code":51,"dataId":4,"value":1}],"initialLevel":1,"maxLevel":99,"name":"Reid","nickname":"","note":"","profile":""},
    {"id":2,"battlerName":"Actor1_2","characterIndex":1,"characterName":"Actor1","classId":1,"equips":[1,0,0,9,0],"faceIndex":1,"faceName":"Actor1","traits":[],"initialLevel":1,"maxLevel":99,"name":"Priscilla","nickname":"","note":"","profile":""},
    {"id":3,"battlerName":"Actor1_3","characterIndex":2,"characterName":"Actor1","classId":5,"equips":[31,0,65,5,0],"faceIndex":2,"faceName":"Actor1","traits":[],"initialLevel":1,"maxLevel":99,"name":"Gale","nickname":"","note":"","profile":""},
    {"id":4,"battlerName":"Actor1_4","characterIndex":3,"characterName":"Actor1","classId":5,"equips":[31,0,0,5,0],"faceIndex":3,"faceName":"Actor1","traits":[],"initialLevel":1,"maxLevel":99,"name":"Michelle","nickname":"","note":"","profile":""},
    {"id":5,"battlerName":"Actor1_5","characterIndex":4,"characterName":"Actor1","classId":2,"equips":[7,0,0,20,0],"faceIndex":4,"faceName":"Actor1","traits":[],"initialLevel":1,"maxLevel":99,"name":"Albert","nickname":"","note":"","profile":""},
    {"id":6,"battlerName":"Actor1_6","characterIndex":5,"characterName":"Actor1","classId":2,"equips":[7,0,65,21,0],"faceIndex":5,"faceName":"Actor1","traits":[],"initialLevel":1,"maxLevel":99,"name":"Kasey","nickname":"","note":"","profile":""},
    {"id":7,"battlerName":"Actor1_7","characterIndex":6,"characterName":"Actor1","classId":3,"equips":[7,0,0,20,0],"faceIndex":6,"faceName":"Actor1","traits":[],"initialLevel":1,"maxLevel":99,"name":"Eliot","nickname":"","note":"","profile":""},
    {"id":8,"battlerName":"Actor1_8","characterIndex":7,"characterName":"Actor1","classId":3,"equips":[7,0,0,20,0],"faceIndex":7,"faceName":"Actor1","traits":[],"initialLevel":1,"maxLevel":99,"name":"Roza","nickname":"","note":"","profile":""}
]
解决方案

下面提供几种不同工具/语言的实现方式,按需选择:

1. 使用JavaScript(Node.js环境)

如果数据原本就是JS变量,直接用Node.js处理并写入文件:

const fs = require('fs');

const toSql = [/* 你的原数据数组 */];

// 提取id和traits字段
const filteredData = toSql.map(item => ({
  id: item.id,
  traits: item.traits
}));

// 写入新的JSON文件
fs.writeFileSync('filtered-result.json', JSON.stringify(filteredData, null, 2));

运行脚本后,会生成filtered-result.json文件,内容为仅包含目标字段的数组。

2. 使用Python

  1. 先将原数据转换为纯JSON格式:去掉const toSql =和末尾分号,保留数组部分,保存为input.json。
  2. 运行以下脚本:
import json

# 读取原文件
with open('input.json', 'r', encoding='utf-8') as f:
    data = json.load(f)

# 提取指定字段
filtered_data = [{'id': item['id'], 'traits': item['traits']} for item in data]

# 写入新文件
with open('output.json', 'w', encoding='utf-8') as f:
    json.dump(filtered_data, f, indent=2)

3. 使用命令行工具jq(快速处理)

若已安装jq(Linux/macOS通过包管理器安装,Windows可下载二进制文件),直接执行命令:

# 假设原数据已保存为input.json
jq 'map({id, traits})' input.json > output.json

执行后,处理结果会直接写入output.json文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 00:24:51