Skip to content

[WIP] Handle JSON data files in import_from_config #627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions hub/management/commands/import_from_config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import json
import os

from django.conf import settings

import pandas as pd
import requests
from jsonpath_ng import jsonpath, parse

from hub.models import AreaData, DataSet

Expand Down Expand Up @@ -66,6 +69,7 @@ def setup(self, import_name, row):
self.cons_col = row["constituency_col"]
self.data_file = settings.BASE_DIR / "data" / row["data_file"]
self.file_type = row["file_type"]
self.jsonpath_expression = row.get("jsonpath_expression", "$") # default to root element
self.area_type = row["area_type"]
self.header_row = row.get("header_row")
self.sheet = row.get("sheet")
Expand Down Expand Up @@ -140,6 +144,22 @@ def get_dataframe(self):
if self.header_row:
kwargs["header"] = int(self.header_row)
df = pd.read_excel(self.data_file, **kwargs)
elif self.file_type == "json":
contents = self.get_file_contents(self.data_file)
try:
tree = json.loads(contents)
except json.JSONDecodeError:
self.stderr.write(f"Invalid JSON file: {self.data_file}")
return None
matches = parse(self.jsonpath_expression).find(tree)
if len(matches) == 1: # jsonpath expression pointed to a single item
items = matches[0]
elif len(matches) > 1: # jsonpath expression matched multiple items, assume the user wants a list of them
items = [match.value for match in matches]
else:
self.stderr.write(f"JSONPath expression {self.jsonpath_expression} returned no matches on {self.data_file}")
return None
df = pd.DataFrame(items)
else:
self.stderr.write(f"Unknown file type: {self.file_type}")
return None
Expand All @@ -148,6 +168,28 @@ def get_dataframe(self):
df = df.astype({self.get_cons_col(): "str"})
return df

def get_file_contents(self, url_or_path):
if os.path.isfile(url_or_path):
try:
with open(url_or_path, 'r', encoding='utf-8') as file:
return file.read()
except Exception as e:
self.stderr.write(f"Error reading local file {url_or_path}: {e}")
return ""

elif url_or_path.startswith('http://') or url_or_path.startswith('https://'):
try:
response = requests.get(url_or_path)
response.raise_for_status()
return response.text
except requests.RequestException as e:
print(f"Error reading remote file {url_or_path}: {e}")
return ""

else:
self.stderr.write(f"Unknown url_or_path passed to get_file_contents: {url_or_path}")
return ""

def get_row_data(self, row, conf):
value = super().get_row_data(row, conf)
if self.multiply_percentage:
Expand Down
Loading
Loading