1

test.jsonPocket 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"
    }

resolved_titleや などのキーの値にアクセスするにはどうすればよいですかword_countidそれらは、それ自体が 内にネストされている と同じように、数値であるオブジェクト内にネストされていlistます。jqを使用してネストされたオブジェクトにアクセスする方法を検索して見つけました。しかし、メイン オブジェクト内の別のオブジェクト内にネストされている値にアクセスするにはどうすればよいlistでしょうか?

また、ID が異なり、連続していないため、再帰は可能ではないと思いますが、間違っている可能性があります。resolved_titleこのデータを使用して、各項目のとのword_count値のみを抽出し、それらを 2 列のスプレッドシートに保存するつもりです。

前もって感謝します!

4

3 に答える 3

0

試してください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"
  }
]
于 2016-03-26T13:19:03.167 に答える
0

演算子を使用し.[]て、配列内のすべての要素 (この場合はすべてのキー) を反復処理できます。以下は、各フィールドを別々の行に出力します。

cat <file_with_json> | jq '.list | .[] | .resolved_title, .word_count'

最初のフィルターはlist要素のみに作用します。2 番目のフィルターはfor every element、最終的に出力がフィールドresolved_title.word_countフィールドだけであることを示しています。これにより、以下が生成されます。

"Title"
"3219"
"Title"
"10549"
于 2016-03-26T13:15:40.650 に答える