Skip to content

Instantly share code, notes, and snippets.

@thistleknot
Created May 6, 2024 17:38
Show Gist options
  • Save thistleknot/76157fbe0c3c723e7ed99ab934e681ae to your computer and use it in GitHub Desktop.
Save thistleknot/76157fbe0c3c723e7ed99ab934e681ae to your computer and use it in GitHub Desktop.
parse json from disk
import json
def load_json_from_disk(file_path):
"""
Load JSON data from disk.
Parameters:
- file_path (str): The path to the JSON file.
Returns:
- data (dict): The JSON data loaded from the file.
"""
try:
with open(file_path, 'r') as file:
data = json.load(file)
return data
except FileNotFoundError:
print("File not found. Please check the file path.")
return None
# Example usage:
file_path = r"C:\Users\User\Documents\wiki\wiki\history\Christian Influences from Greek history.json"
json_data = load_json_from_disk(file_path)
if json_data:
print("JSON data loaded successfully!")
else:
print("Failed to load JSON data.")
def validate_json_structure(data):
"""
Validates the structure of the provided JSON data.
Args:
- data (dict): The JSON data to validate.
Returns:
- bool: True if the JSON structure is properly enclosed, False otherwise.
"""
# Check if the data is a dictionary
if not isinstance(data, dict):
return False
# Check if each top-level key contains a dictionary value
for key, value in data.items():
if not isinstance(value, dict):
return False
return True
# Validate the JSON structure
print(validate_json_structure(sample_json))
for key in sample_json.keys():
print(key)
for key_ in sample_json[key].keys():
print('\t'+key_)
print()
print('\t\t'+sample_json[key][key_]['Summary'])
print()
for quote in sample_json[key][key_]['Quotes']:
print('\t\t\"'+quote+'"')
print()
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment