1
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?

More than 1 year has passed since last update.

Pythonで REST API の data部リクエスト文のString型への変換

Last updated at Posted at 2022-10-18

概要

REST API 経由でデータをやり取りするときに、リクエスト文等をdata部に渡したりしますが、この文をDict型で作成したのち、String型への変更が必要となります。この変更プログラムとなります(備忘録として)。

ちなみに、以下のPythonプログラムの確認対象リクエスト文は Azureの請求データを REST API で取得するためのクエリ・リクエスト文となります。

実行環境

macOS Monterey 12.3.1
python 3.8.12

実行プログラム

payload_json.py
import json

# 確認対象リクエスト文
payload = {
    "type": "Usage",
    "timeframe": "MonthToDate",
    "dataset": {
        "granularity": "Daily",
        "aggregation": {
            "totalCost": {"name": "PreTaxCost", "function": "Sum"}
        },
        "grouping": [
            {"type": "Dimension", "name": "SubscriptionName"}
        ]
    }
}

# データ型の確認
print(payload)
print(type(payload), "\n")

# dict型 から string型への変更
json_payload = json.dumps(payload)

# データ型の再確認
print(json_payload)
print(type(json_payload), "\n")

プログラムの実行

$ python payload_json.py                               
{'type': 'Usage', 'timeframe': 'MonthToDate', 'dataset': {'granularity': 'Daily', 'aggregation': {'totalCost': {'name': 'PreTaxCost', 'function': 'Sum'}}, 'grouping': [{'type': 'Dimension', 'name': 'SubscriptionName'}]}}
<class 'dict'> 

{"type": "Usage", "timeframe": "MonthToDate", "dataset": {"granularity": "Daily", "aggregation": {"totalCost": {"name": "PreTaxCost", "function": "Sum"}}, "grouping": [{"type": "Dimension", "name": "SubscriptionName"}]}}
<class 'str'> 

まとめ

これでAzureの請求データを取得するためのクエリ・リクエスト文の変換準備の完了です。

1
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
1
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?