Reputation: 11
Given the following test.json
that I received as a response from the Pocket API,
{
"complete": 1,
"error": null,
"list": {
"1000055792": {
"excerpt": "Some Text",
"favorite": "0",
"given_title": "Some Title",
"given_url": "Some URL",
"has_image": "0",
"has_video": "0",
"is_article": "1",
"is_index": "0",
"item_id": "1000055792",
"resolved_id": "1000055792",
"resolved_title": "Title",
"resolved_url": "Some URL",
"sort_id": 700,
"status": "1",
"time_added": "1438646514",
"time_favorited": "0",
"time_read": "1439025088",
"time_updated": "1439025090",
"word_count": "10549"
},
"1000102810": {
"excerpt": "Some Text",
"favorite": "0",
"given_title": "Title",
"given_url": "Some URL",
"has_image": "1",
"has_video": "0",
"is_article": "1",
"is_index": "0",
"item_id": "1000102810",
"resolved_id": "1000102810",
"resolved_title": "Title",
"resolved_url": "Resolved URL",
"sort_id": 650,
"status": "1",
"time_added": "1440303789",
"time_favorited": "0",
"time_read": "1440320729",
"time_updated": "1440320731",
"word_count": "3219"
}
How can I access the values of keys like resolved_title
and word_count
. They are nested inside an object which is a number, the same as the id
, which in itself is nested inside list
. I've searched and found a way to access nested objects using jq. But how can I access the values that are nested inside another object within the main list
object?
Also, the IDs are different and not sequential, so I don't think recursion is possible, but I could be wrong. What I'm intending to do with this data is to only extract the resolved_title
and word_count
values for each item and save them to a two-column spreadsheet.
Thanks in advance!
Upvotes: 1
Views: 2159
Reputation: 116967
The following can easily be extended and/or adapted:
> jq ".list[] | {resolved_title, word_count}" input.json
Output:
{
"resolved_title": "Title",
"word_count": "10549"
}
{
"resolved_title": "Title",
"word_count": "3219"
}
Upvotes: 1
Reputation: 6942
Try map()
:
$ cat myfile.json | jq '.list | map({resolved_title: .resolved_title, word_count: .word_count})'
[
{
"resolved_title": "Title",
"word_count": "10549"
},
{
"resolved_title": "Title",
"word_count": "3219"
}
]
Upvotes: 0
Reputation: 21184
You can use the .[]
operator to iterate over all elements in an array (or in this case all the keys). The following will give you output with each field on a separate line:
cat <file_with_json> | jq '.list | .[] | .resolved_title, .word_count'
The first filter operates on only the list
element. The second filter says for every element
and finally the output is just the resolved_title
and .word_count
fields. This produces the following:
"Title"
"3219"
"Title"
"10549"
Upvotes: 0