Similar a esta pregunta quiero extraer algunas piezas de un JSON y construir un uno nuevo solo con la información que necesito. Intenté con este código:

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]

El filtro funciona bien y, de hecho, puedo obtener los valores. El problema es que obtengo TODOS los valores o UNO valor, necesito algunos de ellos, por eso probé con: "ch_epic ['milestone_id'], ch_epic ['stats'] ['num_points']" Pero hubo un error.

¿Cómo puedo lograr esta tarea? A continuación se muestra el código JSON original y quiero extraer: Id, milestone_id y 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"
  }
0
Daniel Luevano Alonso 8 oct. 2019 a las 22:00

3 respuestas

La mejor respuesta

No estoy segura de por qué estás complicando esto ...

Tal vez me perdí algo?

Aquí hay una extracción simple:

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)

Salida

id:             1305552
milestone_id:   195343762
num_points:     5

json:           {"num_points": 5, "id": 1305552, "milestone_id": 195343762}
1
Pitto 8 oct. 2019 a las 19:16

Podría intentar usar el paquete JSON.

Si tiene el JSON guardado en un archivo llamado example.json en el mismo directorio que su script de Python. Puede usar el siguiente código para obtener los campos deseados.

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']})

0
Daniel Ocando 8 oct. 2019 a las 19:41

Adaptarlo a múltiples valores:

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)

salida:

json: [{"id": 1305552, "milestone_id": 195343762, "num_points": 5}, {"id": 1305552, "milestone_id": 195343762, "num_points": 5}]
0
Daniel Luevano Alonso 9 oct. 2019 a las 16:14
58292520