#Parsing JSON file in Python

6 messages · Page 1 of 1 (latest)

fast cargo
#

Hi there,

I am currently trying to parse the enclosed JSON file into a Pandas dataframe but cant seem to get valid results. Here's the code I tried:

import pandas as pd
df_input = pd.read_json("test-state_new.json")
df = pd.read_json(df_input.reset_index().to_json(orient='records'))
df.to_csv("output.csv", index=False)
print(df)

Could you please help me out?

Thanks in advance!!

#

Here's the file. It contains transactions from the Ethereum blockchain (transaction hash, sender and receiver addresses, amounts transferred, etc.

opal mason
#

You have to write custom Python code to handle this @fast cargo

#

However, that being said you can try json_normalize as well . If you are not able to do either , let me know what is the required output. Will help you with some code.

fast cargo
#

Hi Raphael, thanks for your help.In the meantime I solved it myself 🙂

#

import pandas as pd
import json

Load the JSON file

with open('test-state.json') as f:
data = json.load(f)

Create an empty Pandas dataframe

df = pd.DataFrame(columns=['block_number', 'transaction_hash', 'transaction_index', 'from', 'to', 'value', 'timestamp'])

Loop through the JSON data and extract the necessary fields

for block_number, block_data in data['blocks'].items():
for transaction_hash, transaction_data in block_data.items():
for transaction_index, transaction_info in transaction_data.items():
df = df.append({
'block_number': block_number,
'transaction_hash': transaction_hash,
'transaction_index': transaction_index,
'from': transaction_info['from'],
'to': transaction_info['to'],
'value': transaction_info['value'],
'timestamp': transaction_info['timestamp']
}, ignore_index=True)

Print the resulting dataframe

df.to_csv('output_data.csv', sep=',')