This time, I was dealing with a json file that was traversed by AWS Lambda, and I had a lot of trouble, so I will organize the json code a little.
No commentary is included. If you read the flow while collating it with the result, you will understand the meaning.
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("*****'String'.encode('Character code name')*************")
b = s.encode('cp932')
print(type(b))
print("b=",b)
print(b[5:20])
print("*****b'Byte sequence'.decode('Character code name')*************")
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["I"])
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": "Hello!"}'
ke =k.encode('utf-8')
print("ke=",ke)
jke =json.loads(ke)
print(jke)
print("jke['C']=",jke['C'])
print("jke['I']=",jke['I'])
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'])
The output of the above code is as follows.
>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
*****'String'.encode('Character code name')*************
<class 'bytes'>
b= b'{"C": "\\u3042", "\\u3044": {"i": "\\u3046", "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}'
b' "\\u3042", "\\u3'
*****b'Byte sequence'.decode('Character code name')*************
<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': 'Ah', 'I': {'i': 'U', 'j': 2}, 'B': [{'X': 1, 'Y': 10}, {'X': 2, 'Y': 20}]}
Ah
*****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': 'Ah', 'I': {'i': 'U', 'j': 2}, 'B': [{'X': 1, 'Y': 10}, {'X': 2, 'Y': 20}]}
{'i': 'U', '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': 'Ah', 'I': 'Hello!'}
jke['C']=Ah
jke['I']=Hello!
*****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
【reference】 ① Let's master json dumps with Python! (encoding, foramt, datetime) ② How to convert character code with Python [For beginners] ③ Reading / writing JSON files / strings with Python
-Json file can be extracted by specifying it with dictionary type,'heading character': ・ How to identify json files;
<class 'dict'>
{'name': 'tarou', 'age': 23, 'gender': 'man'}
<class 'str'>
{"name": "tarou", "age": 23, "gender": "man"}
** ・ json.loads (converted from str to dict) is different from json.load (read JSON file as a dictionary) ** ** ・ json.dumps (convert from dict to str; can be converted to a character string) is different from json.dump (save dictionary as JSON file) ** ・ Others. .. ..
Recommended Posts