import pandas as pd

"""
Explanation: Pandas convert JSON into a DataFrame
This is a notebook for the medium article How to convert JSON into a Pandas DataFrame?
Please check out article for instructions
License: BSD 2-Clause
End of explanation
"""


df = pd.read_json('data/simple.json')
df

df.info()

"""
Explanation: 1. Reading simple JSON from a local file
End of explanation
"""


URL = 'http://raw.githubusercontent.com/BindiChen/machine-learning/master/data-analysis/027-pandas-convert-json/data/simple.json'
df = pd.read_json(URL)
df

df.info()

"""
Explanation: 2. Reading simple JSON from a URL
End of explanation
"""


df = pd.read_json('data/nested_list.json')
df

import json
# load data using Python JSON module
with open('data/nested_list.json','r') as f:
    data = json.loads(f.read())
    
# Flatten data
df_nested_list = pd.json_normalize(data, record_path =['students'])
df_nested_list

# To include school_name and class
df_nested_list = pd.json_normalize(
    data, 
    record_path =['students'], 
    meta=['school_name', 'class']
)
df_nested_list

"""
Explanation: 3. Flattening nested list from JSON object
End of explanation
"""


### working
import json
# load data using Python JSON module
with open('data/nested_mix.json','r') as f:
    data = json.loads(f.read())
    
# Normalizing data
df = pd.json_normalize(data, record_path =['students'])
df

# Normalizing data
df = pd.json_normalize(
    data, 
    record_path =['students'], 
    meta=[
        'class',
        ['info', 'president'], 
        ['info', 'contacts', 'tel']
    ]
)
df

"""
Explanation: 4. Flattening nested list and dict from JSON object
End of explanation
"""


df = pd.read_json('data/nested_deep.json')
df

type(df['students'][0])

# to install glom inside your python env through the notebook
# pip install glom

from glom import glom
df['students'].apply(lambda row: glom(row, 'grade.math'))

"""
Explanation: 5. Extracting a value from deeply nested JSON
End of explanation
"""


