Python Получение значений из JSON
Есть массив значений:
{
"errors": null,
"events": [
{
"uuid": "5bb28560-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb28560-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb28561-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb28561-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb28562-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb28562-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb28563-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb28563-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb40c00-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb40c00-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb40c01-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb40c01-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb40c02-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb40c02-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb43310-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb43310-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb74050-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb74050-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb74051-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb74051-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true,
"highlight": "medium",
"highlightedField": "importance"
}
},
{
"uuid": "5bb76760-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb76760-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb76761-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb76761-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb851c0-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb851c0-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb851c1-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb851c1-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true,
"highlight": "medium",
"highlightedField": "importance"
}
},
{
"uuid": "5bb851c2-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb851c2-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb878d0-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb878d0-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb96330-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb96330-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb96331-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb96331-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true,
"highlight": "medium",
"highlightedField": "importance"
}
},
{
"uuid": "5bb98a40-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb98a40-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb98a41-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb98a41-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb9b150-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb9b150-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true,
"highlight": "medium",
"highlightedField": "importance"
}
}
],
"totalCount": 21,
"token": "3a31aa69-18e2-4dc9-b16a-a040506b954a"
}
Каким образом из него возможно получить массив по типу
"events" : [
{
"id":"5bb28560-4daa-11ef-9e78-005056b0ed16"
},
{
"id":"5bb28561-4daa-11ef-9e78-005056b0ed16"
}, ....
и т.д. на python
Ответы (2 шт):
Автор решения: Oopss
→ Ссылка
import json
d='''{
"errors": null,
"events": [
{
"uuid": "5bb28560-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb28560-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
{
"uuid": "5bb98a41-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb98a41-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true
}
},
.......
{
"uuid": "5bb9b150-4daa-11ef-9e78-005056b0ed16",
"_meta": {
"id": "5bb9b150-4daa-11ef-9e78-005056b0ed16",
"time": "2024-07-29T13:00:37.0000000Z",
"assetIds": null,
"site_alias": "unknown site_id=null",
"site_name": "unknown site_id=null",
"site_address": "unknown site_id=null",
"site_is_deleted": true,
"highlight": "medium",
"highlightedField": "importance"
}
}
],
"totalCount": 21,
"token": "3a31aa69-18e2-4dc9-b16a-a040506b954a"
}
'''
j=json.loads(d)
res=[]
for i in j["events"]:
res.append({'id':i["_meta"]["id"]})
print({"events":res})
{'events': [{'id': '5bb28560-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb28561-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb28562-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb28563-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb40c00-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb40c01-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb40c02-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb43310-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb74050-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb74051-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb76760-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb76761-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb851c0-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb851c1-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb851c2-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb878d0-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb96330-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb96331-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb98a40-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb98a41-4daa-11ef-9e78-005056b0ed16'}, {'id': '5bb9b150-4daa-11ef-9e78-005056b0ed16'}]}
Автор решения: Dezmonder
→ Ссылка
JSON очень схож с питоновскими словарями, так что идея работы довольно проста:
import json
events = set()
with open('filename') as f:
my_json = json.load(f)
for event in my_json['events']:
events.add({"id": event['_meta']["id"]})