54707


JQコマンドでのJSONの加工をして、特定の値のみ抽出したいのですが、  
マニュアルやサイトを見て、いろいろ試行錯誤したのですが、  
どうしたらよいかわかりません。  
どなたか、助けていただけないでしょうか？

詳細は、下記のようになります。

# 元データ

[code]

    [
      {
        "room_id": 99999999,
        "name": "佐藤一郎",
        "type": "direct",
        "role": "member",
        "sticky": true,
        "unread_num": 2,
        "mention_num": 1,
        "mytask_num": 0,
        "message_num": 28,
        "file_num": 3,
        "task_num": 0,
        "icon_path": "https://appdata.chatwork.com/avatar/99999999/99999999.png",
        "last_update_time": 1556586317
      },
     {
        "room_id": 88888888,
        "name": "山田 花子",
        "type": "direct",
        "role": "member",
        "sticky": false,
        "unread_num": 0,
        "mention_num": 0,
        "mytask_num": 0,
        "message_num": 1,
        "file_num": 0,
        "task_num": 0,
        "icon_path": "https://appdata.chatwork.com/avatar/88888888/88888888.rsz.jpg",
        "last_update_time": 88888888
      }
    ]
    
[/code]

# 得たい結果

[code]

    999999999
    88888888
    
[/code]

# 試したコマンドと実行結果

`$ curl -X GET -H "X-ChatWorkToken: XXXXXXXX"
"https://api.chatwork.com/v2/rooms/" | jq 'room_id'`

[code]

    jq: error: room_id/0 is not defined at <top-level>, line 1:
    room_id
    jq: 1 compile error
    curl: (23) Failed writing body (0 != 16384)
    
[/code]

`$ curl -X GET -H "X-ChatWorkToken: XXXXXXXX"
"https://api.chatwork.com/v2/rooms/" | jq '.room_id'`

[code]

    jq: error (at <stdin>:0): Cannot index array with string "room_id"
    
[/code]

`$ curl -X GET -H "X-ChatWorkToken: XXXXXXXX"
"https://api.chatwork.com/v2/rooms/" | jq '[room_id]'`

[code]

    jq: error: room_id/0 is not defined at <top-level>, line 1:
    [room_id] 
    jq: 1 compile error
    curl: (23) Failed writing body (0 != 16384)
    
[/code]

`$ curl -X GET -H "X-ChatWorkToken: XXXXXXXX"
"https://api.chatwork.com/v2/rooms/" | jq '[.room_id]'`

[code]

    jq: error (at <stdin>:0): Cannot index array with string "room_id"
    
[/code]

`$ curl -X GET -H "X-ChatWorkToken: XXXXXXXX"
"https://api.chatwork.com/v2/rooms/" | jq '[{room_id}]'`

[code]

    jq: error (at <stdin>:0): Cannot index array with string "room_id"
    
[/code]

`$ curl -X GET -H "X-ChatWorkToken: XXXXXXXX"
"https://api.chatwork.com/v2/rooms/" | jq '[{.room_id}]'`

[code]

    jq: error: syntax error, unexpected FIELD (Unix shell quoting issues?) at <top-level>, line 1:
    [{.room_id}]  
    jq: 1 compile error
    curl: (23) Failed writing body (0 != 16384)
    
[/code]


CC BY-SA 4.0
2019-05-04T03:12:30.263
54704
2019-05-04T07:52:45.053
2019-05-04T05:38:59.550
29826
26863
question
json jq
JQコマンドでJSONの特定の値を抽出したい


配列へのアクセスとして `.[]` を使うことで取得できます。

`$ curl -X GET -H "X-ChatWorkToken: XXXXXXXX"
"https://api.chatwork.com/v2/rooms/" | jq '.[].room_id'`

[code]

    99999999
    88888888
    
[/code]


CC BY-SA 4.0
2019-05-04T07:52:45.053
54707
2019-05-04T07:52:45.053
28377
54704
answer