./jqをインストールする
MOONGIFT:JSONを使った開発のお供にどうぞ「jq」
JSONデータ構造を見やすくしてくれるコマンド。
jq is like sed for JSON data – you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.
■環境確認
# cat /etc/redhat-release CentOS release 5.7 (Final) # uname -a Linux xxxxxxx.com 2.6.32-042stab049.6 #1 SMP Mon Feb 6 19:16:12 MSK 2012 i686 i686 i386 GNU/Linux
■ソースのダウンロード(今回の環境ではDownload source)
# cd /usr/local/src/ # wget http://stedolan.github.com/jq/download/source/jq.tgz --2012-11-02 14:02:39-- http://stedolan.github.com/jq/download/source/jq.tgz stedolan.github.com をDNSに問いあわせています... 204.232.175.78 stedolan.github.com|204.232.175.78|:80 に接続しています... 接続しました。 HTTP による接続要求を送信しました、応答を待っています... 200 OK 長さ: 450943 (440K) [application/octet-stream] `jq.tgz' に保存中 100%[===================================================================================================================>] 450,943 339K/s 時間 1.3s 2012-11-02 14:02:41 (339 KB/s) - `jq.tgz' へ保存完了 [450943/450943]
■ソースの展開
# tar xzvf jq.tgz stedolan-jq-95a0074/ stedolan-jq-95a0074/.gitignore .....ずらずら..... stedolan-jq-95a0074/testdata
■フォルダに入ってmake
# cd stedolan-jq-95a0074/ # ls COPYING builtin.h compile.h forkable_stack.h jv.c jv_parse.c jv_unicode.h lexer.l opcode.h parser.gen.info Makefile bytecode.c docs frame_layout.h jv.h jv_parse.h jv_utf8_tables.gen.h locfile.h opcode_list.h parser.h README.md bytecode.h execute.c gen_utf8_tables.py jv_dtoa.c jv_print.c lexer.gen.c main.c parser.gen.c parser.y builtin.c compile.c execute.h jq_test.c jv_dtoa.h jv_unicode.c lexer.gen.h opcode.c parser.gen.h testdata # make gcc -Wextra -Wall -Wno-missing-field-initializers -Wno-unused-parameter -std=gnu99 -ggdb -Wno-unused-function -O -DJQ_DEBUG=0 -o jq parser.gen.c lexer.gen.c opcode.c bytecode.c compile.c execute.c builtin.c jv.c jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c main.c # ll 合計 860 -rw-rw-r-- 1 root root 2957 10月 22 03:52 COPYING .... -rwxrwxr-x 1 root root 292707 11月 2 14:04 jq ....
■「jq」が出来たので「/usr/local/bin/」にシンボリックを貼ってコマンドを登録
# cd /usr/local/bin/ # ln -s /usr/local/src/stedolan-jq-95a0074/jq # ll 合計 5932 ..... lrwxrwxrwx 1 root root 37 11月 2 14:05 jq -> /usr/local/src/stedolan-jq-95a0074/jq .....
■サイトのサンプルを試す
# curl 'http://search.twitter.com/search.json?q=json&rpp=5&include_entities=true' | jq '.results[0]' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 5192 100 5192 0 0 11854 0 --:--:-- --:--:-- --:--:-- 2708k { "to_user_name": null, "to_user_id_str": "0", "to_user_id": 0, "id": 264231272392581120, "geo": null, "from_user_name": "Chris Johnson", "from_user_id_str": "911210857", "from_user_id": 911210857, "from_user": "CHRISpy_Json", "entities": { "user_mentions": [], "urls": [], "hashtags": [ { "indices": [ 103, 121 ], "text": "accomplishedtweet" } ] }, "created_at": "Fri, 02 Nov 2012 05:03:39 +0000", "id_str": "264231272392581120", "iso_language_code": "en", "metadata": { "result_type": "recent" }, "profile_image_url": "http://a0.twimg.com/profile_images/2777986607/635aaa3cc2f805fb9f642d6e616e3ce0_normal.jpeg", "profile_image_url_https": "https://si0.twimg.com/profile_images/2777986607/635aaa3cc2f805fb9f642d6e616e3ce0_normal.jpeg", "source": "<a href="http://twitter.com/download/iphone">Twitter for iPhone</a>", "text": "It's 1 in the morning, i just finished all of my hw, i got to skype ailise and i'm not even that tired #accomplishedtweet", "to_user": null }
出来たー