Pocket API JSON parsing

361 views Asked by At

I am trying to parse the JSON from the Pocket API to keep up with my bookmarks. The JSON recieved after a curl request looks like this:

  "list": {
    "1548784635": {
      "item_id": "1548784635",
      "resolved_id": "1548784635",
      "given_url": "https://stackoverflow.com/questions/28164849/using-jq-to-par
se-and-display-multiple-fields-in-a-json-serially",
      "given_title": "Using jq to parse and display multiple fields in a json se
rially - Stack Ov",
      "favorite": "0",
      "status": "0",
      "time_added": "1542244328",
      "time_updated": "1542244341",
      "time_read": "0",
      "time_favorited": "0",
      "sort_id": 0,
      "resolved_title": "Using jq to parse and display multiple fields in a json
 serially",
      "resolved_url": "https://stackoverflow.com/questions/28164849/using-jq-to-
parse-and-display-multiple-fields-in-a-json-serially",
      "excerpt": "Using jq I'd like to display first and last name serially. Lik
e so -   You can use addition to concatenate strings.",
      "is_article": "1",
      "is_index": "0",
      "has_video": "0",
      "has_image": "1",
      "word_count": "313",
      "lang": "en",
      "top_image_url": "https://cdn.sstatic.net/Sites/stackoverflow/img/apple-to
[email protected]?v=73d79a89bded",
      "tags": {
        "fields": {
          "item_id": "1548784635",
          "tag": "fields"
        },
        "jq": {
          "item_id": "1548784635",
          "tag": "jq"
        },
        "multiple": {
          "item_id": "1548784635",
          "tag": "multiple"
        }
      },
      "authors": {
        "45850780": {
          "item_id": "1548784635",
          "author_id": "45850780",
          "name": "abraham",
          "url": "https://stackoverflow.com/users/26406/abraham"
        },
        "82251593": {
          "item_id": "1548784635",
          "author_id": "82251593",
          "name": "San",
          "url": "https://stackoverflow.com/users/3713971/san"
        }
      },
      "image": {
        "item_id": "1548784635",
        "src": "https://i.stack.imgur.com/rZUli.jpg?s=32&g=1",
        "width": "32",
        "height": "32"
      },
      "images": {
        "1": {
          "item_id": "1548784635",
          "image_id": "1",
          "src": "https://i.stack.imgur.com/rZUli.jpg?s=32&g=1",
          "width": "32",
          "height": "32",
          "credit": "",
          "caption": ""
        }
      },
      "domain_metadata": {
        "name": "Stack Overflow",
        "logo": "https://logo.clearbit.com/stackoverflow.com?size=800",
        "greyscale_logo": "https://logo.clearbit.com/stackoverflow.com?size=800&
greyscale=true"
      },
      "listen_duration_estimate": 121
    },
...

I am trying to get the output to look like this with the uri over the tags (trying to transform to csv):

https://stackoverflow.com/questions/28164849/using-jq-to-parse-and-display-multiple-fields-in-a-json-serially
fields, jq, multiple

I tried this but the tags are nested (the consumer_key & access_token are redacted):

curl https://getpocket.com/v3/get --insecure -X POST -H "Content-Type: application/json" -H "X-Accept: application/json" -d "{\"consumer_key\":\"*\", \"access_token\":\"*\", \"detailType\":\"complete\"}" | jq '.list[] | "\(.given_url) \(.tags)"'
2

There are 2 answers

2
peak On

Using your sample input after rectification to make it valid JSON:

jq -r '.list[] | [.given_url] + (.tags|keys_unsorted) | @csv' input.json

yields:

"https://stackoverflow.com/questions/28164849/using-jq-to-parse-and-display-multiple-fields-in-a-json-serially","fields","jq","multiple"

This is valid CSV. If you don't want all the double quotation marks, you can use string interpolation as in your attempt.

0
d313373d On

I added a ?. This works (even for entries without tags). The access_token and consumer_key are redacted in the curl command.

NOTE: the consumer_key & the access_token have each been replace with *

curl https://getpocket.com/v3/get --insecure -X POST -H "Content-Type: application/json" -H "X-Accept: application/json" -d "{\"consumer_key\":\"80793-861ab2c92e846c8324b417e8\", \"access_token\":\"b6b55049-fe0a-df88-8f78-eb9ccc\", \"detailType\":\"complete\"}" | jq -r '.list[] | [.given_url] + (.tags|keys_unsorted?) | @csv' | less

From the jq manual:

.[]?
       Like .[], but no errors will be output if . is not an array or object.