Skip to content

Instantly share code, notes, and snippets.

@robertdown
Last active September 2, 2023 19:49
Show Gist options
  • Save robertdown/defc2acfd3763cf236db261667d4ad13 to your computer and use it in GitHub Desktop.
Save robertdown/defc2acfd3763cf236db261667d4ad13 to your computer and use it in GitHub Desktop.
Convert a raw JSON URL containing a FHIR CodeSystem Value Set into prepared SQL INSERT commands
import requests
import json
import sys
def get_json_object_from_url(url):
response = requests.get(url)
return json.loads(response.text)
def build_insert_string(json_data):
table = "list_options"
list_id = json_data['id']
columns = [
"list_id"
, "option_id"
, "title"
, "seq"
]
i = 0
strings = [
f"INSERT INTO list_options (list_id, option_id, title, seq) VALUES ('lists', '{list_id}', '{json_data['title']}');"
]
for concept in json_data['concept']:
strings.append(f"INSERT INTO {table} ({', '.join(columns)}) VALUES ('{list_id}', '{concept['code']}', '{concept['display']}', {i});")
i = i + 1
return strings
def build_condition_checks(insert_list, list_id):
insert_list.insert(0, f"#IfNotRow list_options list_id {list_id}")
insert_list.append("#EndIf")
return insert_list
if __name__ == '__main__':
url = sys.argv[1]
raw_value_set = get_json_object_from_url(url)
sql_list = build_insert_string(raw_value_set)
if sys.argv[2] == "true":
sql_list = build_condition_checks(sql_list, raw_value_set['id'])
print("\n".join(sql_list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment