спарсить json данные
Имеется вот такой вид json. Нужно спарсить каждый "description"
{
"orders": [{
"orderStatus": "COMPLETED",
"canSendReceipt": true,
"receiptId": "9cd5f01fd3ed41a384faf04f28e79fb0",
"orderId": "F2102211351463616",
"createdAtMillis": 1613915506904,
"updatedAtMillis": 1613915812732,
"parentOrderId": null,
"currency": "EUR",
"price": 0,
"presentmentAmount": "€0.00",
"items": [{
"description": "Rage 2",
"quantity": 1,
"sellerName": "Bethesda Softworks LLC",
"amount": "€0.00",
"price": 0,
"offerId": "26c8bec5ae4f44b6b942b9564e81cafc",
"namespace": "2736ab9fec9c4a93a7a2872ed5154d4b"
}],
"merchantGroup": "EGS_MKT"
}, {
"orderStatus": "COMPLETED",
"canSendReceipt": true,
"receiptId": "f2131f08c7b84846a20b24b3c7b2c0cd",
"orderId": "F2102211351122654",
"createdAtMillis": 1613915472074,
"updatedAtMillis": 1613915781238,
"parentOrderId": null,
"currency": "EUR",
"price": 0,
"presentmentAmount": "€0.00",
"items": [{
"description": "Absolute Drift",
"quantity": 1,
"sellerName": "Funselektor Labs INC",
"amount": "€0.00",
"price": 0,
"offerId": "0c90d229f27a4251beee8b4776a8fb44",
"namespace": "9d2f484bbec64aa8ad234b3199dcaf1c"
}],
"merchantGroup": "EGS_MKT"
}, {
"orderStatus": "COMPLETED",
"canSendReceipt": true,
"receiptId": "352482c2e2154a9b9fdcc88844ef0c97",
"orderId": "F2101161925140636",
"createdAtMillis": 1610825114658,
"updatedAtMillis": 1610825430967,
"parentOrderId": null,
"currency": "EUR",
"price": 0,
"presentmentAmount": "€0.00",
"items": [{
"description": "STAR WARS™ Battlefront™ II: Celebration Edition",
"quantity": 1,
"sellerName": "Electronic Arts",
"amount": "€0.00",
"price": 0,
"offerId": "ea7721c6c2694e72813d3661bc68a2cb",
"namespace": "b156c3365a5b4cb9a01a5e1108b4e3f4"
}],
"merchantGroup": "EGS_MKT"
}, {
"orderStatus": "COMPLETED",
"canSendReceipt": true,
"receiptId": "8ade8367038f466680ac409314558f18",
"orderId": "F2012230623264687",
"createdAtMillis": 1608704606373,
"updatedAtMillis": 1608704924545,
"parentOrderId": null,
"currency": "EUR",
"price": 0,
"presentmentAmount": "€0.00",
"items": [{
"description": "Metro: 2033 Redux",
"quantity": 1,
"sellerName": "Koch Media",
"amount": "€0.00",
"price": 0,
"offerId": "0dfa5a4398bb44c8b1ac34e5f248fab9",
"namespace": "petunia"
}],
"merchantGroup": "EGS_MKT"
}, {
"orderStatus": "COMPLETED",
"canSendReceipt": true,
"receiptId": "8aa6235d14074563888b7f5f42d5ba36",
"orderId": "F2012180536511836",
"createdAtMillis": 1608269811295,
"updatedAtMillis": 1608270144128,
"parentOrderId": null,
"currency": "EUR",
"price": 0,
"presentmentAmount": "€0.00",
"items": [{
"description": "Cities: Skylines",
"quantity": 1,
"sellerName": "Paradox Interactive",
"amount": "€0.00",
"price": 0,
"offerId": "bf83aee67dd1475fb6bf2c8563f14b70",
"namespace": "6009be9994c2409099588cde6f3a5ed0"
}],
"merchantGroup": "EGS_MKT"
}, {
"orderStatus": "COMPLETED",
"canSendReceipt": true,
"receiptId": "54f46a5a8a5d40fe8aa52a96da3b6ee3",
"orderId": "F2012020608280986",
"createdAtMillis": 1606889308864,
"updatedAtMillis": 1606889627698,
"parentOrderId": null,
"currency": "EUR",
"price": 0,
"presentmentAmount": "€0.00",
"items": [{
"description": "MudRunner",
"quantity": 1,
"sellerName": "Focus Home",
"amount": "€0.00",
"price": 0,
"offerId": "7aea960be7dd4d86a9b30cf5daa03eeb",
"namespace": "6bff1c13a8884b518bd44acaf691ddba"
}],
"merchantGroup": "EGS_MKT"
}, {
"orderStatus": "COMPLETED",
"canSendReceipt": true,
"receiptId": "b321270fbcf34ca8ad41146adad32d14",
"orderId": "A2011161441388942",
"createdAtMillis": 1605537698556,
"updatedAtMillis": 1605537778444,
"parentOrderId": null,
"currency": "EUR",
"price": 799,
"presentmentAmount": "€7.99",
"items": [{
"description": "1,000 V-Bucks",
"quantity": 1,
"sellerName": "Epic Games",
"amount": "€7.99",
"price": 799,
"offerId": "ede05b3c97e9475a8d9be91da65750f0",
"namespace": "fn"
}],
"merchantGroup": "FN_MKT"
}, {
"orderStatus": "COMPLETED",
"canSendReceipt": false,
"receiptId": null,
"orderId": "F2011101619334264",
"createdAtMillis": 1605025173158,
"updatedAtMillis": 1605025173229,
"parentOrderId": null,
"currency": "EUR",
"price": 0,
"presentmentAmount": "€0.00",
"items": [{
"description": "Throwback Axe",
"quantity": 1,
"sellerName": "Epic Games",
"amount": "€0.00",
"price": 0,
"offerId": "4a238a27ef134a60bbaf2fbccb5bfad0",
"namespace": "fn"
}],
"merchantGroup": "FN_MKT"
}, {
"orderStatus": "COMPLETED",
"canSendReceipt": false,
"receiptId": null,
"orderId": "F2009241523041436",
"createdAtMillis": 1600960984697,
"updatedAtMillis": 1600960984767,
"parentOrderId": null,
"currency": "EUR",
"price": 0,
"presentmentAmount": "€0.00",
"items": [{
"description": "Rocket League®",
"quantity": 1,
"sellerName": "Psyonix LLC",
"amount": "€0.00",
"price": 0,
"offerId": "02d44be4c21c4ce094c6151133c91482",
"namespace": "9773aa1aa54f4f7b80e44bef04986cea"
}],
"merchantGroup": "EGS_MKT"
}, {
"orderStatus": "COMPLETED",
"canSendReceipt": true,
"receiptId": "2f0e7924d6fe429488c6fdd7e5c18327",
"orderId": "F2009221625553636",
"createdAtMillis": 1600791955220,
"updatedAtMillis": 1600792258377,
"parentOrderId": null,
"currency": "EUR",
"price": 0,
"presentmentAmount": "€0.00",
"items": [{
"description": "Watch Dogs 2",
"quantity": 1,
"sellerName": "Ubisoft Entertainment",
"amount": "€0.00",
"price": 0,
"offerId": "60d05e5e3a154257a22d6d49810b4b89",
"namespace": "angelonia"
}],
"merchantGroup": "EGS_MKT"
}],
"count": 10,
"start": 0,
"total": 42
}
Пытаюсь спарсить вот так:
r = request.get(html)
data = json.loads(r.text)
disc = data['orders']['items']['description']
print(disc)
Ну и собственно не парсит.
Получаю такую ошибку indices must be integers or slices, not str
Ответы (1 шт):
Автор решения: MaxU
→ Ссылка
Воспользуйтесь dpath:
from dpath import util as dpu # pip install dpath
r = request.get(html)
data = r.json()
res = dpu.values(data, "**/description")
результат:
In [273]: res
Out[273]:
['Rage 2',
'Absolute Drift',
'STAR WARS™ Battlefront™ II: Celebration Edition',
'Metro: 2033 Redux',
'Cities: Skylines',
'MudRunner',
'1,000 V-Bucks',
'Throwback Axe',
'Rocket League®',
'Watch Dogs 2']