Проблемы с доступом к вложенному значению JSON с помощью PYJQ - PullRequest
0 голосов
/ 04 августа 2020
{
"response": {
    "status": "ok",
    "userTier": "developer",
    "total": 18398,
    "startIndex": 1,
    "pageSize": 10,
    "currentPage": 1,
    "pages": 1840,
    "orderBy": "relevance",
    "results": [
        {
            "id": "world/2020/jul/03/coronavirus-the-week-explained",
            "type": "article",
            "sectionId": "world",
            "sectionName": "World news",
            "webPublicationDate": "2020-07-03T11:03:46Z",
            "webTitle": "Coronavirus: the week explained",
            "webUrl": "https://www.theguardian.com/world/2020/jul/03/coronavirus-the-week-explained",
            "apiUrl": "https://content.guardianapis.com/world/2020/jul/03/coronavirus-the-week-explained",
            "fields": {
                "bodyText": "words"
            },
            "isHosted": false,
            "pillarId": "pillar/news",
            "pillarName": "News"
        },
        {
            "id": "music/2020/jun/10/saving-classical-music-from-coronavirus",
            "type": "article",
            "sectionId": "music",
            "sectionName": "Music",
            "webPublicationDate": "2020-06-10T17:38:03Z",
            "webTitle": "Saving classical music from coronavirus | Letters",
            "webUrl": "https://www.theguardian.com/music/2020/jun/10/saving-classical-music-from-coronavirus",
            "apiUrl": "https://content.guardianapis.com/music/2020/jun/10/saving-classical-music-from-coronavirus",
            "fields": {
                "bodyText": "more words"
            },
            "isHosted": false,
            "pillarId": "pillar/arts",
            "pillarName": "Arts"
        }  
    }

Код, который я использую, может получить доступ к WebUrl и webPublicationDate, однако меня интересует значение bodyText. Для доступа к этим значениям я использую pyjq.

data = response. json () query = f'.response .results [] | {{web_url: .webUrl, pub_date: .webPublicationDate}} '

output = pyjq.all (запрос, данные)

^ Этот код извлекает webUrl и PublicationDate. Однако я хочу извлечь bodyText. Как мне написать запрос в том же формате для получения bodyText?

1 Ответ

0 голосов
/ 04 августа 2020

Запрос jq для получения значений .bodyText в виде потока:

.response.results[] | .fields.bodyText
...