サイト上のJSONを取得して加工するスクリプト

  • FileMaker には、JSONを加工する関数も用意されています。
  • 「JSONGetElement」「JSONListKeys」など。
  • その関数を使って、サイト上に公開されているJSONデータを取得して、FileMaker のレコードとして追加するスクリプトを作りました。
URLから挿入 [ $jsonText; "https://jsonplaceholder.typicode.com/todos" ][ダイアログなし]
変数を設定 [ $count; 値:ValueCount (JSONListKeys($jsonText; ""))]
変数を設定 [ $foundID; 値:"" ]
変数を設定 [ $i; 値:1 ]
Loop [フラッシュ: 常に]
    Exit Loop If [ $i > $count]
    変数を設定 $title; 値:JSONGetElement ($jsonText; "[" & $i - 1 & "].title")]
    If [ $title="illo expedita consequatur quia in" ]
        変数を設定 [$foundID; 値:"[" & $i - 1 & "]" ]
        Exit Loop If [ True]
    End If
    変数を設定 [ $i; 値:$i + 1 ]
End Loop
If [ $foundID ≠ ""]
    変数を設定 [$result; 値:JSONGetElement ($jsonText; $foundID)]
    カスタムダイアログを表示 [タイトル: "一致したデータ"; メッセージ: $result; デフォルトボタン: 「OK」 確定: 「はい」] 
    新規レコード/検索条件
    フィールド設定 [ json_get_from_site::id; JSONGetElement( $result; "id")]
    フィールド設定 [ json_get_from_site::title; JSONGetElement( $result; "title")]
Else [ ]
    カスタムダイアログを表示 [タイトル: “結果”; メッセージ: "該当するデータがありません"; デフォルトボタン: 「OK」, 確定: 「はい」] 
End If

動作イメージです。

  • todosのJSONデータを取得
  • 取得したJSONより、titleが「illo expedita consequatur quia in」のデータを取得
  • 合致したデータをダイアログで表示
  • 新規レコードを作成して、合致したデータより、id、titleをフィールドに設定

JSONデータは、JSONPlaceholder – Free Fake REST API の todos のデータを利用させていただきました。

同様の処理をPythonで書くと、下記のような内容になります。

import requests

def main():
    url = "https://jsonplaceholder.typicode.com/todos"
    response = requests.get(url)
    response.raise_for_status()
    todos = response.json()

    # titleが「illo expedita consequatur quia in」のものだけを抽出
    target_title = "illo expedita consequatur quia in"
    filtered = [todo for todo in todos if todo.get("title") == target_title]

    for todo in filtered:
        print(todo)

if __name__ == "__main__":
    main()
    PAGE TOP