In [1]:
import pandas as pd
import json

In [2]:
s = '{"col1":{"row1":1,"row2":2,"row3":3},"col2":{"row1":"a","row2":"x","row3":"\u3042"}}'

In [3]:
df_s = pd.read_json(s)

In [4]:
print(df_s)


      col1 col2
row1     1    a
row2     2    x
row3     3    あ

In [5]:
s_single_quote = "{'col1':{'row1':1,'row2':2,'row3':3},'col2':{'row1':'a','row2':'x','row3':'\u3042'}}"

In [6]:
# df_s_single_quote = pd.read_json(s_single_quote)
# ValueError: Expected object or value

In [7]:
print(pd.read_json(s_single_quote.replace("'", '"')))


      col1 col2
row1     1    a
row2     2    x
row3     3    あ

In [8]:
df_f = pd.read_json('data/src/sample_from_pandas_columns.json')

In [9]:
print(df_f)


      col1 col2
row1     1    a
row2     2    x
row3     3    あ

In [10]:
df_gzip = pd.read_json('data/src/sample_from_pandas_columns.gz', compression='infer')

In [11]:
print(df_gzip)


      col1 col2
row1     1    a
row2     2    x
row3     3    あ

In [12]:
df_s_index = pd.read_json(s, orient='index')

In [13]:
print(df_s_index)


     row1 row2 row3
col1    1    2    3
col2    a    x    あ

In [14]:
# df_s_split = pd.read_json(s, orient='split')
# ValueError: JSON data had unexpected key(s): col2, col1

In [15]:
s_jsonl = '''{"col1":1,"col2":"a"}
{"col1":2,"col2":"x"}
{"col1":3,"col2":"\u3042"}'''

In [16]:
print(s_jsonl)


{"col1":1,"col2":"a"}
{"col1":2,"col2":"x"}
{"col1":3,"col2":"あ"}

In [17]:
df_s_jsonl = pd.read_json(s_jsonl, orient='records', lines=True)

In [18]:
print(df_s_jsonl)


   col1 col2
0     1    a
1     2    x
2     3    あ

In [19]:
s_nested = '{"OTHER": "x", "DATA": {"col1":{"row1":1,"row2":2},"col2":{"row1":"a","row2":"x"}}}'

In [20]:
print(pd.read_json(s_nested))


                            DATA OTHER
col1      {'row1': 1, 'row2': 2}     x
col2  {'row1': 'a', 'row2': 'x'}     x

In [21]:
d = json.loads(s_nested)

In [22]:
print(d)


{'OTHER': 'x', 'DATA': {'col1': {'row1': 1, 'row2': 2}, 'col2': {'row1': 'a', 'row2': 'x'}}}

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


<class 'dict'>

In [24]:
d_target = d['DATA']

In [25]:
print(d_target)


{'col1': {'row1': 1, 'row2': 2}, 'col2': {'row1': 'a', 'row2': 'x'}}

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


<class 'dict'>

In [27]:
s_target = json.dumps(d_target)

In [28]:
print(s_target)


{"col1": {"row1": 1, "row2": 2}, "col2": {"row1": "a", "row2": "x"}}

In [29]:
print(type(s_target))


<class 'str'>

In [30]:
df_target = pd.read_json(s_target)

In [31]:
print(df_target)


      col1 col2
row1     1    a
row2     2    x

In [32]:
df_target2 = pd.read_json(json.dumps(json.loads(s_nested)['DATA']))

In [33]:
print(df_target2)


      col1 col2
row1     1    a
row2     2    x