Python 提取JSON标记并构建新的JSON
与此类似,我想提取一些JSON片段,并根据我需要的信息构建一个新的JSON。 我尝试使用以下代码:Python 提取JSON标记并构建新的JSON,python,json,python-3.x,Python,Json,Python 3.x,与此类似,我想提取一些JSON片段,并根据我需要的信息构建一个新的JSON。 我尝试使用以下代码: wanted_milestones_ids = [ch_epic['milestone_id'],ch_epic['stats']['num_points'] for ch_epic in ch_epics if ch_epic['milestone_id'] is not None and set(CLUBHOUSE_PROJECT_IDS).intersection(ch_epic['pro
wanted_milestones_ids = [ch_epic['milestone_id'],ch_epic['stats']['num_points'] for ch_epic in ch_epics if ch_epic['milestone_id'] is not None and set(CLUBHOUSE_PROJECT_IDS).intersection(ch_epic['project_ids']) and ch_epic['id'] == 16919]
过滤器工作正常,实际上我可以得到值。问题是,我获得了所有值或一个值,我需要其中的一些值,这就是为什么我尝试使用“Chu epic['milestone\u id'],Chu epic['stats']['num_points']”但出现了一个错误
我怎样才能完成这项任务?下面是原始的JSON代码,我想提取:Id、milestone_Id和num_points
{
"archived": false,
"started": false,
"entity_type": "epic",
"labels": [
{
"entity_type": "label",
"id": 1305552,
"created_at": "2018-10-01T16:37:13Z",
"updated_at": "2018-11-13T14:25:23Z",
"name": "random name",
"color": "#e885d1",
"external_id": null,
"archived": false
}
],
"external_tickets": [],
"mention_ids": [],
"member_mention_ids": [],
"project_ids": [
2022
],
"completed_at_override": null,
"started_at": null,
"completed_at": null,
"name": "Randome Name",
"completed": false,
"state": "to do",
"milestone_id": 195343762,
"requested_by_id": "Goofy",
"epic_state_id": 50230002327020,
"started_at_override": null,
"updated_at": "2019-08-08T21:19:00Z",
"group_mention_ids": [],
"support_tickets": [],
"follower_ids": [
"Donald"
],
"owner_ids": [],
"external_id": null,
"id": 16965,
"position": 961,
"deadline": null,
"stats": {
"num_points_done": 1,
"average_cycle_time": 32223231706,
"num_stories_unstarted": 2,
"last_story_update": "2019-10-02T12:24:08Z",
"num_points_started": 1,
"num_points_unstarted": 3,
"num_stories_started": 1,
"num_stories_unestimated": 2,
"average_lead_time": 3305323270,
"num_points": 5,
"num_stories_done": 2
},
"created_at": "2019-03-28T16:39:50Z"
}
我不知道你为什么要把这个复杂化 也许我错过了什么 下面是一个简单的摘录:
import json
data = r'{ "archived": false, "started": false, "entity_type": "epic", "labels": [ { "entity_type": "label", "id": 1305552, "created_at": "2018-10-01T16:37:13Z", "updated_at": "2018-11-13T14:25:23Z", "name": "random name", "color": "#e885d1", "external_id": null, "archived": false } ], "external_tickets": [], "mention_ids": [], "member_mention_ids": [], "project_ids": [ 2022 ], "completed_at_override": null, "started_at": null, "completed_at": null, "name": "Randome Name", "completed": false, "state": "to do", "milestone_id": 195343762, "requested_by_id": "Goofy", "epic_state_id": 50230002327020, "started_at_override": null, "updated_at": "2019-08-08T21:19:00Z", "group_mention_ids": [], "support_tickets": [], "follower_ids": [ "Donald" ], "owner_ids": [], "external_id": null, "id": 16965, "position": 961, "deadline": null, "stats": { "num_points_done": 1, "average_cycle_time": 32223231706, "num_stories_unstarted": 2, "last_story_update": "2019-10-02T12:24:08Z", "num_points_started": 1, "num_points_unstarted": 3, "num_stories_started": 1, "num_stories_unestimated": 2, "average_lead_time": 3305323270, "num_points": 5, "num_stories_done": 2 }, "created_at": "2019-03-28T16:39:50Z" }'
data = data.replace('\n', '')
json_data = json.loads(data)
id = json_data['labels'][0]['id']
milestone_id = json_data['milestone_id']
num_points = json_data['stats']['num_points']
print("id:\t\t{}".format(id))
print("milestone_id:\t{}".format(milestone_id))
print("num_points:\t{}".format(num_points))
output = {'id':id, 'milestone_id':milestone_id, 'num_points':num_points}
final_output = json.dumps(output)
print("\njson:\t\t" + final_output)
输出
id: 1305552
milestone_id: 195343762
num_points: 5
json: {"num_points": 5, "id": 1305552, "milestone_id": 195343762}
您可以尝试使用JSON包 如果将JSON保存在一个名为example.JSON的文件中,该文件与python脚本位于同一目录中。您可以使用以下代码来获取所需的字段
import json
with open("example.json", "r") as read_file:
data = json.load(read_file)
output_file = json.dumps({'id':data['id'], 'milestone_id':data['milestone_id'], 'num_points':data['stats']['num_points']})
使其适应多个值:
import json
data = r'[{ "archived": false, "started": false, "entity_type": "epic", "labels": [ { "entity_type": "label", "id": 1305552, "created_at": "2018-10-01T16:37:13Z", "updated_at": "2018-11-13T14:25:23Z", "name": "random name", "color": "#e885d1", "external_id": null, "archived": false } ], "external_tickets": [], "mention_ids": [], "member_mention_ids": [], "project_ids": [ 2022 ], "completed_at_override": null, "started_at": null, "completed_at": null, "name": "Randome Name", "completed": false, "state": "to do", "milestone_id": 195343762, "requested_by_id": "Goofy", "epic_state_id": 50230002327020, "started_at_override": null, "updated_at": "2019-08-08T21:19:00Z", "group_mention_ids": [], "support_tickets": [], "follower_ids": [ "Donald" ], "owner_ids": [], "external_id": null, "id": 16965, "position": 961, "deadline": null, "stats": { "num_points_done": 1, "average_cycle_time": 32223231706, "num_stories_unstarted": 2, "last_story_update": "2019-10-02T12:24:08Z", "num_points_started": 1, "num_points_unstarted": 3, "num_stories_started": 1, "num_stories_unestimated": 2, "average_lead_time": 3305323270, "num_points": 5, "num_stories_done": 2 }, "created_at": "2019-03-28T16:39:50Z" },{ "archived": false, "started": false, "entity_type": "epic", "labels": [ { "entity_type": "label", "id": 1305552, "created_at": "2018-10-01T16:37:13Z", "updated_at": "2018-11-13T14:25:23Z", "name": "random name", "color": "#e885d1", "external_id": null, "archived": false } ], "external_tickets": [], "mention_ids": [], "member_mention_ids": [], "project_ids": [ 2022 ], "completed_at_override": null, "started_at": null, "completed_at": null, "name": "Randome Name", "completed": false, "state": "to do", "milestone_id": 195343762, "requested_by_id": "Goofy", "epic_state_id": 50230002327020, "started_at_override": null, "updated_at": "2019-08-08T21:19:00Z", "group_mention_ids": [], "support_tickets": [], "follower_ids": [ "Donald" ], "owner_ids": [], "external_id": null, "id": 16965, "position": 961, "deadline": null, "stats": { "num_points_done": 1, "average_cycle_time": 32223231706, "num_stories_unstarted": 2, "last_story_update": "2019-10-02T12:24:08Z", "num_points_started": 1, "num_points_unstarted": 3, "num_stories_started": 1, "num_stories_unestimated": 2, "average_lead_time": 3305323270, "num_points": 5, "num_stories_done": 2 }, "created_at": "2019-03-28T16:39:50Z" }]'
data = data.replace('\n', '')
json_data = json.loads(data)
output = []
for milestone in json_data:
id = milestone['labels'][0]['id']
milestone_id = milestone['milestone_id']
num_points = milestone['stats']['num_points']
# print("id:\t\t{}".format(id))
# print("milestone_id:\t{}".format(milestone_id))
# print("num_points:\t{}".format(num_points))
output.append({'id':id, 'milestone_id':milestone_id, 'num_points':num_points})
final_output = json.dumps(output)
print("\njson:\t\t" + final_output)
输出:
json: [{"id": 1305552, "milestone_id": 195343762, "num_points": 5}, {"id": 1305552, "milestone_id": 195343762, "num_points": 5}]
谢谢它起作用了,我把它适应了多种价值观,而且效果很好。谢谢。非常高兴它对您有用@DanielLuevanoAlonso!尝试运行您的示例,我得到了:output_file=json.dumps({'id':data['id'],'milestone_id':data['milestone_id'],'num_points':data['stats']['num_points']})TypeError:列表索引必须是整数或片,而不是str