1

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!

  • what language are you doing this in? javascript? you added the "command-line" tag, so are you using node.js? or some other language? – Ken Bellows Mar 26 '16 at 13:04
  • @KenB The question and the tags both mention [tag:jq] – tripleee Mar 26 '16 at 13:09
  • ah sorry, I thought `jq` meant jQuery, now I see it's a command line util for navigating JSON – Ken Bellows Mar 26 '16 at 13:11
  • Related / relevant? http://stackoverflow.com/questions/27562424/jq-nested-object-extract-top-level-id-and-lift-a-value-from-internal-object – tripleee Mar 26 '16 at 13:17
  • @tripleee Yes, I'd referred to your thread before, but the problem you had was with nested objects one level deep. I'd tried that, but it didn't work, unfortunately. – Akhil Unnikrishnan Mar 26 '16 at 15:30

3 Answers3

1

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"
}
peak
  • 105,803
  • 17
  • 152
  • 177
0

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"
tddmonkey
  • 20,798
  • 10
  • 58
  • 67
0

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"
  }
]
Ken Bellows
  • 6,711
  • 13
  • 50
  • 78
  • It worked, thank you. A little off-topic but may I ask another question related to the command line, if you don't mind? Can I feed the output from a certain command into another command as input, without having to save the intermediate results as a file. What I mean specifically is that, can I use the command that downloads my data from Pocket and pass it onto the code you've written, using the `>` operator? Thanks again. :) – Akhil Unnikrishnan Mar 26 '16 at 15:37
  • You can pipe `|` the output of Pocket to jq as long as Pocket supports sending to stdout – tddmonkey Mar 26 '16 at 15:43
  • @AkhilUnnikrishnan yeah, any command that outputs text to the terminal can be fed into `jq` with the pipe character (`|`), and that includes any command that you use `>` for to redirect its output into a file – Ken Bellows Mar 26 '16 at 16:30