0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

今日からjq

Last updated at Posted at 2024-11-30

今日からjq

まずはコマンド理解から

では一発目

[root@centos8_1 ~]# curl "http://geoapi.heartrails.com/api/json?method=searchByPostal&postal=1530062" -s | jq -r
{
  "response": {
    "location": [
      {
        "city": "目黒区",
        "city_kana": "めぐろく",
        "town": "三田",
        "town_kana": "みた",
        "x": "139.71233",
        "y": "35.640156",
        "prefecture": "東京都",
        "postal": "1530062"
      }
    ]
  }
}

ではここから さらに情報を絞っていきます

[root@centos8_1 ~]# curl "http://geoapi.heartrails.com/api/json?method=searchByPostal&postal=1530062" -s | jq -r .response
{
  "location": [
    {
      "city": "目黒区",
      "city_kana": "めぐろく",
      "town": "三田",
      "town_kana": "みた",
      "x": "139.71233",
      "y": "35.640156",
      "prefecture": "東京都",
      "postal": "1530062"
    }
  ]
}
[root@centos8_1 ~]#
[root@centos8_1 ~]# curl "http://geoapi.heartrails.com/api/json?method=searchByPostal&postal=1530062" -s | jq -r .response[]
[
  {
    "city": "目黒区",
    "city_kana": "めぐろく",
    "town": "三田",
    "town_kana": "みた",
    "x": "139.71233",
    "y": "35.640156",
    "prefecture": "東京都",
    "postal": "1530062"
  }
]
[root@centos8_1 ~]#

まずまず情報を絞ってこれましたね

ここからがややこしい

'シングルコレクションとパイプが必要になります

多分ここで言わんとしていることは
最初のresponse
次のlocationの[]の中の
までを1つのパイプで
次のパイプで
その中身を. [.prefecture, .town,.city]を出してよ

っている理解。

[root@centos8_1 ~]# curl "http://geoapi.heartrails.com/api/json?method=searchByPostal&postal=1530062" -s | jq -r '.response.location[] | [.prefecture, .town,
 .city]'
[
  "東京都",
  "三田",
  "目黒区"
]

またこんなやり方もある

[root@centos8_1 ~]# curl "http://geoapi.heartrails.com/api/json?method=searchByPostal&postal=1530062" -s | jq -r '.response.location[] | .city'
目黒区

では次は天気予報情報でやってみよう

[root@centos8_1 ~]# curl  https://www.jma.go.jp/bosai/forecast/data/forecast/130000.json -s | jq -r .[].timeSeries[].areas[].area
{
  "name": "東京地方",
  "code": "130010"
}
{
  "name": "伊豆諸島北部",
  "code": "130020"
}
{
  "name": "伊豆諸島南部",
  "code": "130030"
}
{
  "name": "小笠原諸島",
  "code": "130040"
}
{
  "name": "東京地方",
  "code": "130010"
}
{
  "name": "伊豆諸島北部",
  "code": "130020"
}
{
  "name": "伊豆諸島南部",
  "code": "130030"
}
{
  "name": "小笠原諸島",
  "code": "130040"
}
{
  "name": "東京",
  "code": "44132"
}
{
  "name": "大島",
  "code": "44172"
}
{
  "name": "八丈島",
  "code": "44263"
}
{
  "name": "父島",
  "code": "44301"
}
{
  "name": "東京地方",
  "code": "130010"
}
{
  "name": "伊豆諸島北部",
  "code": "130020"
}
{
  "name": "伊豆諸島南部",
  "code": "130030"
}
{
  "name": "小笠原諸島",
  "code": "130040"
}
{
  "name": "東京",
  "code": "44132"
}
{
  "name": "大島",
  "code": "44172"
}
{
  "name": "八丈島",
  "code": "44263"
}
{
  "name": "父島",
  "code": "44301"
}
[root@centos8_1 ~]#

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?