Created
April 15, 2022 00:36
-
-
Save dixonwhitmire/62cd91628db42645776baa3a1416860e to your computer and use it in GitHub Desktop.
JSON Path Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from fhir.resources.patient import Patient | |
from fhir.resources import construct_fhir_element | |
from jsonpath_ng import jsonpath, parse | |
from jsonpath_ng.ext import parser | |
with open('Patient.json', 'r') as f: | |
pat_dict = json.load(f) | |
patient: Patient = construct_fhir_element("Patient", pat_dict) | |
#print(bundle) | |
#Name Format Identifier Type Identifier Group | |
de_id_config = { | |
"operations": [ | |
{ | |
"taskType": "Delete elsewhere", | |
} | |
], | |
} | |
## Update field | |
name_path = parser.parse('name[?use].text') | |
for name_entry in name_path.find(pat_dict): | |
#name_entry.value = "abcd" | |
print(name_entry.path) | |
name_entry.context.value[str(name_entry.path)] = "abcd" | |
print(name_entry.context.value) | |
#print(name_entry) | |
## Delete from list | |
name_path = parser.parse('name[?use=="common"]') | |
#values_to_delete = [] | |
#for name_entry in name_path.find(pat_dict): | |
# #name_entry.value = "abcd" | |
# values_to_delete.append(name_entry.value) | |
# ctx = name_entry.context.value | |
# | |
#for value_to_delete in values_to_delete: | |
# ctx.remove(value_to_delete) | |
## Delete from list | |
ext_path = parser.parse('meta.extension[*]') | |
#ext_condition_path = parser.parse('valueCodeableConcept.coding[?code="ADT"]') | |
#for ext_entry in ext_path.find(pat_dict): | |
# ADT_ext_entry = ext_condition_path.find(ext_entry.value) | |
# if len(ADT_ext_entry) > 0: | |
# ext_entry.context.value.remove(ext_entry.value) | |
## Delete elsewhere | |
to_delete_path = parser.parse('name') | |
#to_delete_path = parser.parse('name[?use]') | |
#to_delete_path = parser.parse('name[*].given') | |
condition_path = parser.parse('gender') | |
condition = 'female' | |
for condition_entry in condition_path.find(pat_dict): | |
if condition_entry.value == condition: | |
for to_delete_entry in to_delete_path.find(pat_dict): | |
print(to_delete_entry.value) | |
if type(to_delete_entry.context.value) == list: | |
to_delete_entry.context.value.remove(to_delete_entry.value) | |
else: | |
del to_delete_entry.context.value[str(to_delete_entry.path)] | |
## Reject resource | |
#rejection_condition_path = parser.parse('identifier[*].type.coding[?code=="MR"]') | |
#for rejection_condition_entry in rejection_condition_path.find(pat_dict): | |
# print("resource rejected!!!") | |
## Whitelist top level fields | |
#fields_to_keep = ['name', 'identifier'] | |
#for field in list(pat_dict.keys()): | |
# if field not in fields_to_keep: | |
# del pat_dict[field] | |
print(json.dumps(pat_dict)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment