使用PYJQ访问嵌套JSON值时出现问题


{
"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值作为流检索的jq查询是:

.response.results[] | .fields.bodyText

最新更新