Pocket API JSON解析

时间:2018-11-15 02:01:00

标签: api parsing jq bookmarks pocket

我正在尝试从Pocket API解析JSON,以跟上我的书签。在curl请求后收到的JSON如下所示:

  "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
uch-icon@2.png?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
    },
...

我试图通过标签上的uri使输出看起来像这样(试图转换为csv):

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

我尝试过此操作,但是标签是嵌套的(consumer_key和access_token已编辑):

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 个答案:

答案 0 :(得分:0)

在更正后使用示例输入使其成为有效的JSON:

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

产量:

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

这是有效的CSV。如果您不希望所有的双引号都可以,请尝试使用字符串插值。

答案 1 :(得分:-1)

我加了一个?。这有效(即使没有标签的条目也是如此)。在curl命令中修改了access_token和consumer_key。

注意:consumer_key和access_token均已替换为*

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

摘自jq手册:

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