Skip to content

Commit

Permalink
add xml and json importer
Browse files Browse the repository at this point in the history
  • Loading branch information
ThiagoTrabach committed Aug 2, 2023
1 parent 31f7c2f commit 7e481bb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions fhir_utils/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import json
import xmltodict

def json_to_dict(json_file_path):
try:
with open(json_file_path, 'r') as file:
data = json.load(file)
return data
except FileNotFoundError:
print(f"File '{json_file_path}' not found.")
except json.JSONDecodeError:
print(f"Unable to parse JSON file: '{json_file_path}'.")
except Exception as e:
print(f"An error occurred while importing JSON: {e}")


def xml_to_dict(xml_file_path):
try:
with open(xml_file_path, 'r') as file:
xml_data = file.read()
return xmltodict.parse(xml_data)
except FileNotFoundError:
print(f"Error: XML file not found at '{xml_file_path}'")
return None
except Exception as e:
print(f"Error: Unable to parse XML file. Reason: {str(e)}")
return None


def save_to_json(dict, json_path,):
with open(json_path, "w") as file:
json.dump(dict, file)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ rsa==4.9
six==1.16.0
uritemplate==4.1.1
urllib3==1.26.16
xmltodict==0.13.0

0 comments on commit 7e481bb

Please sign in to comment.