将 HL7 v2 转换为 JSON [英] Converting HL7 v2 to JSON

查看:50
本文介绍了将 HL7 v2 转换为 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将 HL7 v2(旧的 EDI 格式)消息转换为 JSON,这样我可以使它们在 Apache Drill 下可处理并在 Parquet 下可压缩.

I am looking to convert HL7 v2 (older EDI format) messages to JSON, so I could make them processable under Apache Drill and compressible under Parquet.

我查看了 HAPI,但我没有找到实用程序非 XML HL7 到 JSON 的转换.

I looked into HAPI, but I am not having luck finding utility for non-XML HL7 to JSON conversion.

有人对图书馆有建议或参考吗?

Does anyone have a suggestion or a reference to a library?

推荐答案

只需使用 HAPI 转换为 XML.下面的代码需要 Saxon,因为 XML-to-JSON 需要 XSLT 2.0,但是如果您已经有了将 XML 转换为 JSON 的方法,那么您只需要前两行,它们完全是 HAPI.当然,您应该在本地下载 XSLT 用于生产.:-)

Just use HAPI to convert to XML. The code below requires Saxon, because the XML-to-JSON requires XSLT 2.0, but if you already have a method to convert XML to JSON, then you just need the first two lines, which are entirely HAPI. You should download the XSLT locally for production, of course. :-)

String convertHL7ToJson(Message message) {
    try {
        DefaultXMLParser xmlParser = new DefaultXMLParser(new CanonicalModelClassFactory("2.6"));
        String xml = xmlParser.encode(message);
        Transformer xmlTransformer = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", null).newTransformer(
            new StreamSource(new StringReader(readFileFromURL("https://github.com/bramstein/xsltjson/raw/master/conf/xml-to-json.xsl")))
            );
          StringWriter result = new StringWriter();
          xmlTransformer.transform(
             new StreamSource(new StringReader(xml)), new StreamResult(result)
          );
          return result.toString();
    } catch (Throwable t) {
        t.printStackTrace();
    }
    return null;
}

String readFileFromURL(String url) {
    InputStream is = null;
    try {
        return new Scanner(is = new URL(url).openStream(), "UTF-8").useDelimiter("\A").next();
    } catch (Throwable t) {
        t.printStackTrace();
    } finally {
        if(is != null)
            try {
                is.close();
            } catch (Throwable ignored){}
    }
    return null;
}

这会产生这样的输出:

"ORM_O01":{"MSH":{"MSH.1":"|","MSH.2":"^~\&","MSH.3":{"HD.1":"TEST"},"MSH.4":{"HD.1":"TEST000","HD.2":"BL"},...

这篇关于将 HL7 v2 转换为 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆