Although the HL7 standard makes it less verbose when sending the information along, I prefer working with JSON objects as opposed to pipe delimited strings. If you are in the same boat, then you can use the following two python functions to convert an HL7 message to an easier to digest/understand. (Note these two functsion depend on the hl7apy python library)
Yes, these functions return a python dictionary and not a JSON object, but you can trivially convert a dictionary to a JSON string.
import json
from hl7apy.parser import parse_message
# Taken from http://hl7apy.org/tutorial/index.html#elements-manipulation
s = """MSH|^~\&|GHH_ADT||||20080115153000||ADT^A01^ADT_A01|0123456789|P|2.5||||AL
EVN||20080115153000||AAA|AAA|20080114003000
PID|1||566-554-3423^^^GHH^MR||EVERYMAN^ADAM^A|||M|||2222 HOME STREET^^ANN ARBOR^MI^^USA||555-555-2004~444-333-222|||M
NK1|1|NUCLEAR^NELDA^W|SPO|2222 HOME STREET^^ANN ARBOR^MI^^USA"""
# Convert it
d = hl7_str_to_dict(s)
# Dump it as a JSON string
print json.dumps(d)
Hope this helps someone who appreciates new data representations more than old data representations ;)
Any idea what'd cause this when scanning in a file:
ReplyDeleteAttributeError: 'NoneType' object has no attribute 'lower'
When I scanned in your string (without """) it worked.
Had to put str(c.name).lower() on line 46
DeleteThanks for the find! I've updated the gist.
DeleteThanks for writing this. Do you have any code for going back to SIU message?
ReplyDeleteUnfortunately, I only wrote it going one way. If you write it, I'd love to see it!
DeleteMy only suggestion tool is that mirth connect which convert any hl7 messages like ADT or scheduling segment and for laboratories segment easily to any format like json, MySQL, xml etc..
ReplyDeleteGreat post! Thanks!
ReplyDelete