In [1]:
import json
from collections import OrderedDict
import pprint
In [2]:
s = r'{"C": "\u3042", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}'
In [3]:
print(s)
In [4]:
d = json.loads(s)
In [5]:
pprint.pprint(d, width=40)
In [6]:
print(type(d))
In [7]:
od = json.loads(s, object_pairs_hook=OrderedDict)
In [8]:
pprint.pprint(od)
In [9]:
b = s.encode()
In [10]:
print(b)
In [11]:
print(type(b))
In [12]:
db = json.loads(b)
In [13]:
pprint.pprint(db, width=40)
In [14]:
print(type(db))
In [15]:
sb = b.decode()
In [16]:
print(sb)
In [17]:
print(type(sb))
In [18]:
dsb = json.loads(sb)
In [19]:
pprint.pprint(dsb, width=40)
In [20]:
print(type(dsb))
In [21]:
sb_u = b.decode('unicode-escape')
In [22]:
print(sb_u)
In [23]:
print(type(sb_u))
In [24]:
dsb_u = json.loads(sb_u)
In [25]:
pprint.pprint(dsb_u, width=40)
In [26]:
print(type(dsb_u))
In [27]:
with open('data/src/test.json') as f:
print(f.read())
In [28]:
with open('data/src/test.json') as f:
df = json.load(f)
In [29]:
pprint.pprint(df, width=40)
In [30]:
print(type(df))
In [31]:
pprint.pprint(d, width=40)
In [32]:
print(d['A'])
In [33]:
print(d['A']['i'])
In [34]:
print(d['B'])
In [35]:
print(d['B'][0])
In [36]:
print(d['B'][0]['X'])
In [37]:
value = d['B'][1]['Y']
print(value)
In [38]:
# print(d['D'])
# KeyError: 'D'
In [39]:
print(d.get('D'))
In [40]:
d['C'] = 'ん'
pprint.pprint(d, width=40)
In [41]:
d.pop('C')
pprint.pprint(d, width=40)
In [42]:
d['C'] = 'あ'
pprint.pprint(d, width=40)
In [43]:
sd = json.dumps(d)
In [44]:
print(sd)
In [45]:
print(type(sd))
In [46]:
pprint.pprint(od)
In [47]:
sod = json.dumps(od)
In [48]:
print(sod)
In [49]:
print(type(sod))
In [50]:
print(json.dumps(d, separators=(',', ':')))
In [51]:
print(json.dumps(d, separators=(' / ', '-> ')))
In [52]:
print(json.dumps(d, indent=4))
In [53]:
print(json.dumps(od))
In [54]:
print(json.dumps(od, sort_keys=True))
In [55]:
print(json.dumps(od, ensure_ascii=False))
In [56]:
with open('data/dst/test2.json', 'w') as f:
json.dump(d, f, indent=4)
In [57]:
with open('data/dst/test2.json') as f:
print(f.read())
In [58]:
d_new = {'A': 100, 'B': 'abc', 'C': 'あいうえお'}
In [59]:
with open('data/dst/test_new.json', 'w') as f:
json.dump(d_new, f, indent=2, ensure_ascii=False)
In [60]:
with open('data/dst/test_new.json') as f:
print(f.read())
In [61]:
with open('data/dst/test_new.json') as f:
d_update = json.load(f, object_pairs_hook=OrderedDict)
In [62]:
print(d_update)
In [63]:
d_update['A'] = 200
d_update.pop('B')
d_update['D'] = 'new value'
In [64]:
print(d_update)
In [65]:
with open('data/dst/test_new_update.json', 'w') as f:
json.dump(d_update, f, indent=2, ensure_ascii=False)
In [66]:
with open('data/dst/test_new_update.json') as f:
print(f.read())
In [67]:
with open('data/src/test.json') as f:
print(f.read())
In [68]:
with open('data/src/test.json', encoding='unicode-escape') as f:
print(f.read())
In [69]:
print(b)
In [70]:
print(b.decode())
In [71]:
print(b.decode(encoding='unicode-escape'))
In [72]:
d = {"A": 100, "B": 'abc', "C": 'あいうえお'}
In [73]:
print(str(d))
In [74]:
# print(json.loads(str(d)))
# JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
In [75]:
print(json.dumps(d))
In [76]:
print(json.loads(json.dumps(d)))
In [77]:
print(type(json.loads(json.dumps(d))))