LoginSignup
1
1

More than 3 years have passed since last update.

【json入門】いやいやハマったので。。。♬

Last updated at Posted at 2020-06-22

今回、AWSのLambdaでTranscribeしたjsonファイルを扱っていて、すごく苦労したので、ちょっとjsonのコード整理しておきます。
※本題は別途まとめます

jsonってほんと素晴らしいですね♬

解説は入れません。
結果と照合しつつ、流れを読んでいただければ、意味が分かるように思います。
※まとめにまとめて書きました

import json

s = r'{"C": "\u3042", "\u3044": {"i": "\u3046", "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}'

print("******************")
print(type(s))
print("s=",s)
print(s[5:20])

print("*****'文字列'.encode('文字コード名')*************")
b = s.encode('cp932')
print(type(b))
print("b=",b)
print(b[5:20])

print("*****b'バイト列'.decode('文字コード名')*************")
c = b.decode('cp932')
print(type(c))
print("c=",c)
print(c[5:20])

print("*****json.loads(s)*************")
dict = json.loads(s)
print(type(dict))
print("dict=",dict)
print(dict["C"])

print("*****json.dumps(dict)*************")
enc = json.dumps(dict)
print(type(enc))
print("enc=",enc)
print(enc[5:20])

print("*****json.loads(enc)*************")
dict2 = json.loads(enc)
print(type(dict2))
print("dict2=",dict2)
print(dict2["い"])

print("*****json.dumps(dict2)*************")
enc = json.dumps(dict2)
print(type(enc))
print("enc=",enc)
print(enc[5:20])

print("*****b.decode("utf-8")*************")
db = b.decode("utf-8")
print(type(db))
print("db=",db)
print(db[5:20])

print("*****k.encode('utf-8')*************")
k = r'{"C": "\u3042", "\u3044": "こんにちは!"}'
ke =k.encode('utf-8')
print("ke=",ke)
jke =json.loads(ke)
print(jke)
print("jke['C']=",jke['C'])
print("jke['い']=",jke['い'])

print("*****json.dumps(dict, default=expireEncoda)*************")
from datetime import datetime
def expireEncoda(object):
    if isinstance(object, datetime):
        return object.isoformat()
dict = {"now": datetime.now()}
enc = json.dumps(dict, default=expireEncoda)
print("dict=",dict)
print("dict['now']=",dict['now'])
print("enc=",enc)

denc =json.loads(enc)
print("denc['now']=",denc['now'])

出力結果

上記コードの出力は以下のとおりです。

>python ex_jason.py
******************
<class 'str'>
s= {"C": "\u3042", "\u3044": {"i": "\u3046", "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}
 "\u3042", "\u3
*****'文字列'.encode('文字コード名')*************
<class 'bytes'>
b= b'{"C": "\\u3042", "\\u3044": {"i": "\\u3046", "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}'
b' "\\u3042", "\\u3'
*****b'バイト列'.decode('文字コード名')*************
<class 'str'>
c= {"C": "\u3042", "\u3044": {"i": "\u3046", "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}
 "\u3042", "\u3
*****json.loads(s)*************
<class 'dict'>
dict= {'C': 'あ', 'い': {'i': 'う', 'j': 2}, 'B': [{'X': 1, 'Y': 10}, {'X': 2, 'Y': 20}]}
あ
*****json.dumps(dict)*************
<class 'str'>
enc= {"C": "\u3042", "\u3044": {"i": "\u3046", "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}
 "\u3042", "\u3
*****json.loads(enc)*************
<class 'dict'>
dict2= {'C': 'あ', 'い': {'i': 'う', 'j': 2}, 'B': [{'X': 1, 'Y': 10}, {'X': 2, 'Y': 20}]}
{'i': 'う', 'j': 2}
*****json.dumps(dict2)*************
<class 'str'>
enc= {"C": "\u3042", "\u3044": {"i": "\u3046", "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}
 "\u3042", "\u3
*****b.decode("utf-8")*************
<class 'str'>
db= {"C": "\u3042", "\u3044": {"i": "\u3046", "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}
 "\u3042", "\u3
*****k.encode('utf-8')*************
ke= b'{"C": "\\u3042", "\\u3044": "\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf\xef\xbc\x81"}'
{'C': 'あ', 'い': 'こんにちは!'}
jke['C']= あ
jke['い']= こんにちは!
*****json.dumps(dict, default=expireEncoda)*************
dict= {'now': datetime.datetime(2020, 6, 22, 21, 13, 57, 761568)}
dict['now']= 2020-06-22 21:13:57.761568
enc= {"now": "2020-06-22T21:13:57.761568"}
denc['now']= 2020-06-22T21:13:57.761568

【参考】
Pythonでjson dumpsを使いこなそう!(encoding、foramt、datetime)
Pythonで文字コードを変換する方法【初心者向け】
PythonでJSONファイル・文字列の読み込み・書き込み

まとめ

・jsonファイルは辞書型、'見出し文字':で指定して抽出できる
・jsonファイルの見分け方;

<class 'dict'>
{'name': 'tarou', 'age': 23, 'gender': 'man'}
<class 'str'>
{"name": "tarou", "age": 23, "gender": "man"}

・json.loads(str→dictに変換)は、json.load(JSONファイルを辞書として読み込み)とは違う
・json.dumps(dict→strに変換;文字列に出来る)は、json.dump(辞書をJSONファイルとして保存)とは違う
・その他...

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