Pocket API JSON解析 [英] Pocket API JSON parsing

查看:141
本文介绍了Pocket API JSON解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
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):

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

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

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)"'

推荐答案

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

Using your sample input after rectification to make it valid 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.如果您不希望所有的双引号都可以,请尝试使用字符串插值.

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

这篇关于Pocket API JSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆