1
1

More than 1 year has passed since last update.

Google Apps Script: HTTP APIを呼び出してJSONを取得する方法

Posted at

Google Apps Script(GAS)で外部のHTTP APIを呼び出し、そのレスポンスからJSONデータを取り出す方法です。

GASで外部サイトにHTTPリクエストを送るには、UrlFetchApp.fetchを使います。その戻り値はHTTPResponseです。HTTPResponse.getContentTextでレスポンスボディーが得られるので、それをJSONパースすることでデータが得られます。

function fetchJson(url) {
    const res = UrlFetchApp.fetch(url);
    return JSON.parse(res.getContentText());
}

使い方

function example() {
    const users = fetchJson("https://jsonplaceholder.typicode.com/users");
    console.log(users[0].name); //=> "Leanne Graham"
}
1
1
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
1
1