CLI로 json을 인코딩하는 jq 명령

6875 단어 jq

내용


  • curl 명령으로 API를 두드려도 응답이 깨지기 어려워
  • jq 명령이라는 편리한 도구가있는 것 같습니다

  • 과제 : 터미널에서 curl 명령으로 API를 쳤을 때 응답이 깨집니다.


    
    $ curl http://weather.livedoor.com/forecast/webservice/json/v1\?city=130010 | python -m json.tool
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  8249    0  8249    0     0   207k      0 --:--:-- --:--:-- --:--:--  211k
    {
        "pinpointLocations": [
            {
                "link": "http://weather.livedoor.com/area/forecast/1310100",
                "name": "\u5343\u4ee3\u7530\u533a"
            },
            {
                "link": "http://weather.livedoor.com/area/forecast/1310200",
                "name": "\u4e2d\u592e\u533a"
            },
            {
                "link": "http://weather.livedoor.com/area/forecast/1310300",
                "name": "\u6e2f\u533a"
            },
            {
                "link": "http://weather.livedoor.com/area/forecast/1310400",
                "name": "\u65b0\u5bbf\u533a"
            },
            {
                "link": "http://weather.livedoor.com/area/forecast/1310500",
                "name": "\u6587\u4eac\u533a"
            },
            {
                "link": "http://weather.livedoor.com/area/forecast/1310600",
                "name": "\u53f0\u6771\u533a"
            },
    # 略
    

    구누 누 ...

    jq라는 도구 사용


    
    $ brew install jq
    Updating Homebrew...
    ==> Auto-updated Homebrew!
    Updated 1 tap (homebrew/core).
    ==> Updated Formulae
    kobalt                                                                                                                               makeself
    
    ==> Installing dependencies for jq: oniguruma
    ==> Installing jq dependency: oniguruma
    ==> Downloading https://homebrew.bintray.com/bottles/oniguruma-6.1.3.sierra.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Pouring oniguruma-6.1.3.sierra.bottle.tar.gz
    🍺  /usr/local/Cellar/oniguruma/6.1.3: 17 files, 1.3M
    ==> Installing jq
    ==> Downloading https://homebrew.bintray.com/bottles/jq-1.5_2.sierra.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Pouring jq-1.5_2.sierra.bottle.tar.gz
    🍺  /usr/local/Cellar/jq/1.5_2: 18 files, 957.9K
    ➜  ~ which jq
    /usr/local/bin/jq
    

    한 번 더 두드리는
    
    curl http://weather.livedoor.com/forecast/webservice/json/v1\?city=130010 | jq
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  8249    0  8249    0     0   184k      0 --:--:-- --:--:-- --:--:--  187k
    {
      "pinpointLocations": [
        {
          "link": "http://weather.livedoor.com/area/forecast/1310100",
          "name": "千代田区"
        },
        {
          "link": "http://weather.livedoor.com/area/forecast/1310200",
          "name": "中央区"
        },
        {
          "link": "http://weather.livedoor.com/area/forecast/1310300",
          "name": "港区"
        },
        {
          "link": "http://weather.livedoor.com/area/forecast/1310400",
          "name": "新宿区"
        }
    # 略
    

    그래, 괜찮아.

    좋은 웹페이지 즐겨찾기