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)


{"C": "\u3042", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}

In [4]:
d = json.loads(s)

In [5]:
pprint.pprint(d, width=40)


{'A': {'i': 1, 'j': 2},
 'B': [{'X': 1, 'Y': 10},
       {'X': 2, 'Y': 20}],
 'C': 'あ'}

In [6]:
print(type(d))


<class 'dict'>

In [7]:
od = json.loads(s, object_pairs_hook=OrderedDict)

In [8]:
pprint.pprint(od)


OrderedDict([('C', 'あ'),
             ('A', OrderedDict([('i', 1), ('j', 2)])),
             ('B',
              [OrderedDict([('X', 1), ('Y', 10)]),
               OrderedDict([('X', 2), ('Y', 20)])])])

In [9]:
b = s.encode()

In [10]:
print(b)


b'{"C": "\\u3042", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}'

In [11]:
print(type(b))


<class 'bytes'>

In [12]:
db = json.loads(b)

In [13]:
pprint.pprint(db, width=40)


{'A': {'i': 1, 'j': 2},
 'B': [{'X': 1, 'Y': 10},
       {'X': 2, 'Y': 20}],
 'C': 'あ'}

In [14]:
print(type(db))


<class 'dict'>

In [15]:
sb = b.decode()

In [16]:
print(sb)


{"C": "\u3042", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}

In [17]:
print(type(sb))


<class 'str'>

In [18]:
dsb = json.loads(sb)

In [19]:
pprint.pprint(dsb, width=40)


{'A': {'i': 1, 'j': 2},
 'B': [{'X': 1, 'Y': 10},
       {'X': 2, 'Y': 20}],
 'C': 'あ'}

In [20]:
print(type(dsb))


<class 'dict'>

In [21]:
sb_u = b.decode('unicode-escape')

In [22]:
print(sb_u)


{"C": "あ", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}

In [23]:
print(type(sb_u))


<class 'str'>

In [24]:
dsb_u = json.loads(sb_u)

In [25]:
pprint.pprint(dsb_u, width=40)


{'A': {'i': 1, 'j': 2},
 'B': [{'X': 1, 'Y': 10},
       {'X': 2, 'Y': 20}],
 'C': 'あ'}

In [26]:
print(type(dsb_u))


<class 'dict'>

In [27]:
with open('data/src/test.json') as f:
    print(f.read())


{"C": "\u3042", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}

In [28]:
with open('data/src/test.json') as f:
    df = json.load(f)

In [29]:
pprint.pprint(df, width=40)


{'A': {'i': 1, 'j': 2},
 'B': [{'X': 1, 'Y': 10},
       {'X': 2, 'Y': 20}],
 'C': 'あ'}

In [30]:
print(type(df))


<class 'dict'>

In [31]:
pprint.pprint(d, width=40)


{'A': {'i': 1, 'j': 2},
 'B': [{'X': 1, 'Y': 10},
       {'X': 2, 'Y': 20}],
 'C': 'あ'}

In [32]:
print(d['A'])


{'i': 1, 'j': 2}

In [33]:
print(d['A']['i'])


1

In [34]:
print(d['B'])


[{'X': 1, 'Y': 10}, {'X': 2, 'Y': 20}]

In [35]:
print(d['B'][0])


{'X': 1, 'Y': 10}

In [36]:
print(d['B'][0]['X'])


1

In [37]:
value = d['B'][1]['Y']
print(value)


20

In [38]:
# print(d['D'])
# KeyError: 'D'

In [39]:
print(d.get('D'))


None

In [40]:
d['C'] = 'ん'
pprint.pprint(d, width=40)


{'A': {'i': 1, 'j': 2},
 'B': [{'X': 1, 'Y': 10},
       {'X': 2, 'Y': 20}],
 'C': 'ん'}

In [41]:
d.pop('C')
pprint.pprint(d, width=40)


{'A': {'i': 1, 'j': 2},
 'B': [{'X': 1, 'Y': 10},
       {'X': 2, 'Y': 20}]}

In [42]:
d['C'] = 'あ'
pprint.pprint(d, width=40)


{'A': {'i': 1, 'j': 2},
 'B': [{'X': 1, 'Y': 10},
       {'X': 2, 'Y': 20}],
 'C': 'あ'}

In [43]:
sd = json.dumps(d)

In [44]:
print(sd)


{"A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}], "C": "\u3042"}

In [45]:
print(type(sd))


<class 'str'>

In [46]:
pprint.pprint(od)


OrderedDict([('C', 'あ'),
             ('A', OrderedDict([('i', 1), ('j', 2)])),
             ('B',
              [OrderedDict([('X', 1), ('Y', 10)]),
               OrderedDict([('X', 2), ('Y', 20)])])])

In [47]:
sod = json.dumps(od)

In [48]:
print(sod)


{"C": "\u3042", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}

In [49]:
print(type(sod))


<class 'str'>

In [50]:
print(json.dumps(d, separators=(',', ':')))


{"A":{"i":1,"j":2},"B":[{"X":1,"Y":10},{"X":2,"Y":20}],"C":"\u3042"}

In [51]:
print(json.dumps(d, separators=(' / ', '-> ')))


{"A"-> {"i"-> 1 / "j"-> 2} / "B"-> [{"X"-> 1 / "Y"-> 10} / {"X"-> 2 / "Y"-> 20}] / "C"-> "\u3042"}

In [52]:
print(json.dumps(d, indent=4))


{
    "A": {
        "i": 1,
        "j": 2
    },
    "B": [
        {
            "X": 1,
            "Y": 10
        },
        {
            "X": 2,
            "Y": 20
        }
    ],
    "C": "\u3042"
}

In [53]:
print(json.dumps(od))


{"C": "\u3042", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}

In [54]:
print(json.dumps(od, sort_keys=True))


{"A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}], "C": "\u3042"}

In [55]:
print(json.dumps(od, ensure_ascii=False))


{"C": "あ", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}

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())


{
    "A": {
        "i": 1,
        "j": 2
    },
    "B": [
        {
            "X": 1,
            "Y": 10
        },
        {
            "X": 2,
            "Y": 20
        }
    ],
    "C": "\u3042"
}

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())


{
  "A": 100,
  "B": "abc",
  "C": "あいうえお"
}

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)


OrderedDict([('A', 100), ('B', 'abc'), ('C', 'あいうえお')])

In [63]:
d_update['A'] = 200
d_update.pop('B')
d_update['D'] = 'new value'

In [64]:
print(d_update)


OrderedDict([('A', 200), ('C', 'あいうえお'), ('D', 'new value')])

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())


{
  "A": 200,
  "C": "あいうえお",
  "D": "new value"
}

In [67]:
with open('data/src/test.json') as f:
    print(f.read())


{"C": "\u3042", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}

In [68]:
with open('data/src/test.json', encoding='unicode-escape') as f:
    print(f.read())


{"C": "あ", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}

In [69]:
print(b)


b'{"C": "\\u3042", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}'

In [70]:
print(b.decode())


{"C": "\u3042", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}

In [71]:
print(b.decode(encoding='unicode-escape'))


{"C": "あ", "A": {"i": 1, "j": 2}, "B": [{"X": 1, "Y": 10}, {"X": 2, "Y": 20}]}

In [72]:
d = {"A": 100, "B": 'abc', "C": 'あいうえお'}

In [73]:
print(str(d))


{'A': 100, 'B': 'abc', 'C': 'あいうえお'}

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))


{"A": 100, "B": "abc", "C": "\u3042\u3044\u3046\u3048\u304a"}

In [76]:
print(json.loads(json.dumps(d)))


{'A': 100, 'B': 'abc', 'C': 'あいうえお'}

In [77]:
print(type(json.loads(json.dumps(d))))


<class 'dict'>