Below is the example of my jsonline data
{"allocation":{"duration":1,"rewards":[{"expiryDate":"2021-01-01 23:59:59","rewardSubType":"Boxes"}]},"MODULE":"a","APPLICATION":"g","GUID2":"gh-gh-gh","METHOD":"Expensive","timestamp":"2021-01-01 07:00:00.497"}
{"allocation":{"duration":2,"rewards":[{"expiryDate":"2021-01-02 23:59:59","rewardSubType":"Keys"}]},"MODULE":"a","APPLICATION":"b","GUID":"gh-gh-gh","METHOD":"Free","timestamp":"2021-01-02 07:00:00.497"}
I have run the following code
import pandas as pd
import json
from pandas import json_normalize
with open('C:\\Vodacom\\KPI Dashboard\\ussd_vservices_audit.jsonl') as f:
lines = f.read().splitlines()
df_inter = pd.DataFrame(lines)
df_inter.columns = ['json_element']
df_inter['json_element'].apply(json.loads)
df_final = pd.json_normalize(df_inter['json_element'].apply(json.loads))
df_final
That results in
However, I need to further split up the newly created allocation.rewards (old "rewards") column into separate columns too. It now has square brackets and single quotes too so I have no idea how to do this.
Please assist Thanks!
Amazing answer thank you - exactly what I needed! But now I have found when running against my full dataset, I have some rows of data that don't have a rewards section/column as below:
{"offer":null,"MODULE":"purchase","APPLICATION":"new","GUID":"cf-83-11-001","METHOD":"Upsize","timestamp":"2021-02-01 06:00:01.158"}
This causes me to then have the error TypeError: object of type 'float' has no len()