by shigemk2

当面は技術的なことしか書かない

jqコマンドが存外に便利かもしれないという話

JSONを表示さすURLをcurlで叩いたときはこんな感じになると思うんです。

$ curl http://restro.nalwaya.com/restaurants/search.json\?city\=Chicago
[{"name":"Dolalli","thumb_url_image":"http://restro.nalwaya.com/system/restaurants/avatars/000/000/001/thumb/Restaurant-Blue-2-icon.png?1374505105","food_type":"American","latitude":37.0,"longitude":-22.0,"desc":"Traditional American joint "},{"name":"Pizzamast","thumb_url_image":"http://restro.nalwaya.com/system/restaurants/avatars/000/000/002/thumb/Restaurant-Blue-icon.png?1374505157","food_type":"Italian","latitude":30.0,"longitude":-122.0,"desc":"200 type of Pizzas"},{"name":"More Pizza","thumb_url_image":"http://restro.nalwaya.com/system/restaurants/avatars/000/000/003/thumb/pizza-slice-icon.png?1374505545","food_type":"American","latitude":111.0,"longitude":-10.0,"desc":"Pizza at your door step"}]

JSONが1行で表示されちゃっているから、何がなんだか分からないってことはないけど、
すごく読みづらい。

で、これをきれいに表示さすのがjqコマンドであります。

homebrewなら、このコマンド一発でおkだお。

$ brew install jq
$ curl http://restro.nalwaya.com/restaurants/search.json\?city\=Chicago | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   712    0   712    0     0   1173      0 --:--:-- --:--:-- --:--:--  1225
[
  {
    "desc": "Traditional American joint ",
    "longitude": -22,
    "latitude": 37,
    "food_type": "American",
    "thumb_url_image": "http://restro.nalwaya.com/system/restaurants/avatars/000/000/001/thumb/Restaurant-Blue-2-icon.png?1374505105",
    "name": "Dolalli"
  },
  {
    "desc": "200 type of Pizzas",
    "longitude": -122,
    "latitude": 30,
    "food_type": "Italian",
    "thumb_url_image": "http://restro.nalwaya.com/system/restaurants/avatars/000/000/002/thumb/Restaurant-Blue-icon.png?1374505157",
    "name": "Pizzamast"
  },
  {
    "desc": "Pizza at your door step",
    "longitude": -10,
    "latitude": 111,
    "food_type": "American",
    "thumb_url_image": "http://restro.nalwaya.com/system/restaurants/avatars/000/000/003/thumb/pizza-slice-icon.png?1374505545",
    "name": "More Pizza"
  }
]