Convert a LIXI2 message from XML to JSON (or vice versa)
A LIXI2 message can be serialised in either XML or JSON format. LIXI2 XML and JSON formats hold identical information and are structured in exactly the same way, it is only the data serialisation format that is different. This means that a LIXI2 message in the XML format can be converted to JSON (or in the other direction from JSON to XML) without any loss of data.
LIXI has developed a LIXI2 compliant XML to JSON converter. There are some subtleties to the XML/JSON conversion that mean using a generic converter (such as an online conversion tool) will not always convert the message correctly.
The LIXI2 converter is available as a feature of our LIXI Python Package.
Read a LIXI2 message using the LIXI Python Package
1) Install the LIXI Python Package
pip install lixi
2) Download the LIXI2 schema for your LIXI2 message
Links to download LIXI2 schemas can be found here: LIXI standards Release History.
3) Read your LIXI message using the LIXI Python Package
Sample LIXI2 messages for Single Applicant Credit Application or Joint Applicant Credit Application are available on our Credit Application Tutorials (available to logged in members). We also have a repository of LIXI2 Samples in LIXILab. Read your LIXI2 message using the following code (being sure to use the correct file paths for your LIXI2 message and LIXI2 schema).
import lixi lixi_message = lixi.read_message( message_path="message.xml", schema_path="LIXI-CAL-2_6_36-Annotated.xsd" )
Convert XML to JSON using the LIXI Python Package
We now have our LIXI2 message object. We can convert this object to JSON using the to_json function. We can then save the JSON message to file using the save function, or write to the terminal using the pretty_print function.
lixi_message.to_json() lixi_message.save("message.json") lixi_message.pretty_print()
Convert JSON to XML using the LIXI Python Package
Lets say we started with our LIXI2 message in the JSON format and we wanted to read the file, convert it to XML and then save the file as XML or display it on the terminal.
The JSON message can be read in exactly the same way as the XML message, using the read_message function.
lixi_message = lixi.read_message( message_path="message.json", schema_path="LIXI-CAL-2_6_36-Annotated.xsd" ) lixi_message.to_xml() lixi_message.save("result.xml") lixi_message.pretty_print()
Convert from a string in memory instead of from a file
Let's say we started with our LIXI2 message as a string rather than a file, and we wanted to read it in to convert the format? In this situation, the message has already been loaded into a string 'message_as_string' so it is as simple as changing the input parameter used from 'message_path' to 'message' during the read_message().
lixi_message = lixi.read_message( message=message_as_string, schema_path="LIXI-CAL-2_6_36-Annotated.xsd" )
Convert to a string in memory instead of to a file
Finally, let's say we wanted to convert our LIXI2 message (as an in memory json string) to an in memory xml string rather than to an xml file. Now all we need to do is use to_string() on the lixi message XML object.
lixi_message = lixi.read_message( message=data, schema_path="LIXI-CAL-2_6_36-Annotated.xsd" ) lixi_message.to_xml() lixi_message.to_string()
The detailed package documentation includes a Quickstart Guide that takes you through all the key functionality (available to logged in users - any member or licensee can register for a free account here).
Finally, feel free to contact any member of the LIXI team (or use our contact form) and we will be happy to help with more information and a demonstration.
Related Blog Posts
John Matthews, LIXI Technical Lead
First Published: March 12, 2020 | Last Updated: March 23, 2021