LoginSignup
5
3

More than 5 years have passed since last update.

boto3 でオブジェクトを JSON に変換

Last updated at Posted at 2017-11-24

boto3 (AWS SDK for Python) で、オブジェクトを JSON に変換する方法です。 json.dumps(response) だと、datetime 型に対応していません。

参考にしたページ
boto3 (AWS SDK for Python) メモ

Arch Linux で、bson のインストール

sudo pacman -S python-pymongo

Ubuntu 17.10 で、bson のインストール

sudo apt install python3-pymongo
boto3_json.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   boto3_json.py
#
#                       Nov/24/2017
#
import sys
import json
import boto3
from bson import json_util
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")

client = boto3.client('ec2')
response = client.describe_instances()
print(type(response))

res_json = json.dumps(response, default=json_util.default)
print(type(res_json))

print(res_json)
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------

参考
Raspbian での boto3 のインストール

sudo pip3 install boto3
5
3
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
5
3