Reading JSON files in Python

JSON (JavaScript Object Notation) is a text format for storing and transporting data. It is “self-describing” and easy to understand. JASON files can be loaded in Python. Two methods are used here: first loading the data using pandas DataFrame and then using the python ‘open() file’ function.

				
					# Reuirements:
import pandas as pd
from pandas import Series, DataFrame
import json
				
			
				
					# METHOD 1. Read data from fileand display in dataframe:
df_components = pd.read_json('Geophysics/components.json')
# print(df.to_string()) 
# print(df)
df_components.head()
				
			
load jason
				
					# Set the wellboreName name as index  if that's the feature focus# 
df_components.set_index('wellboreName', inplace=True)
# View the first five elements and see how the dataframe was changed
df_components.head()
				
			
load_jason2
				
					# METHOD 2. Use Open() file function
with open('Geophysics/components.json') as f:
  data = json.load(f)
print(data)
				
			
				
					[{'wellboreName': 'WB08-03-96', 'componentName': 'HALITE', 'topDepth': 3479.016, 'bottomDepth': 3480.56}, {'wellboreName': 'WB08-03-96', 'componentName': 'MARL', 'topDepth': 3490.359, 'bottomDepth': 3492.108}, {'wellboreName': 'WB08-03-96', 'componentName': 'SAND', 'topDepth': 4069.7752, 'bottomDepth': 4078.572}, {'wellboreName': 'WB05-10-20', 'componentName': 'CLAYSTONE', 'topDepth': 2650.3, 'bottomDepth': 2652.54}, {'wellboreName': 'WB05-10-20', 'componentName': 'CHALK', 'topDepth': 2890.96, 'bottomDepth': 3001.89}, {'wellboreName': 'WB05-10-20', 'componentName': 'SHALE', 'topDepth': 3010.51, 'bottomDepth': 3027.654}, {'wellboreName': 'WB05-10-20', 'componentName': 'SILTSTONE', 'topDepth': 7075.37, 'bottomDepth': 7081.26}, {'wellboreName': 'WB05-10-20', 'componentName': 'LIMESTONE', 'topDepth': 7101.134, 'bottomDepth': 7102.292}]
				
			

Leave a Reply