API Connect

 View Only
Expand all | Collapse all

API Connect v10: Convert JSON request.body to XML with namespaces

  • 1.  API Connect v10: Convert JSON request.body to XML with namespaces

    Posted Wed September 28, 2022 05:34 AM

    Hi guys,

    I´m trying to convert an incoming JSON request.body to XML format with namespaces.

    The XML element TransportOrder should look like this after conversion from JSON:

    <yq1:TransportOrder xmlns:yq1="urn:XYZ.de:logistics:transportorder">

    Using the parse and json-to-xml policies in the assembly only leads to the namespace being appended to the TransportOrder element as:
    <TransportOrder>
        <data>some data</data>
        <a>some data</a>
        <b>some data</b>
        <c>some data</c>
        <_xmlns_yq1>urn:XYZ.de:logistics:transportorder</_xmlns_yq1>
    </TransportOrder>


    How should the original request in JSON look like so that the namespace xmlns:yq1="urn:XYZ.de:logistics:transportorder" is added to the TransportOrder element?


    Is there a way to achieve this without using a map policy?


    Thanks and best regards,
    Henri


    ------------------------------
    Henri Unruh
    ------------------------------


  • 2.  RE: API Connect v10: Convert JSON request.body to XML with namespaces

    Posted Thu September 29, 2022 09:07 AM
    I think this can be achieved by Map assemble. To get the sample, I used a free JSON to XML (or vise versa) with namespaces (freeformatter.com)

    ------------------------------
    Samir Almousawi
    ------------------------------



  • 3.  RE: API Connect v10: Convert JSON request.body to XML with namespaces

    Posted Thu September 29, 2022 09:21 AM

    Thank you for your response. However I have not yet found a way to do this via Map policy.
    How would that map look like? Hardcode the namespace to the root element somehow?

    Regards,




    ------------------------------
    Henri Unruh
    ------------------------------



  • 4.  RE: API Connect v10: Convert JSON request.body to XML with namespaces

    Posted Thu September 29, 2022 11:35 AM

    I found a way to do it. The badgerfish notation describes it here: https://badgerfish.ning.com/.

    You basically need to add a property to the JSON element like this:

    {
    	"yq1:TransportOrder": {
            "@xmlns": {
                "yq1": "urn:XYZ.de:logistics:transportorder"
            },


    The gateway will then know how to handle this correctly.



    ------------------------------
    Henri Unruh
    ------------------------------



  • 5.  RE: API Connect v10: Convert JSON request.body to XML with namespaces

    Posted Fri June 30, 2023 10:47 AM

    Hi Henri,

    How does the rest of the JSON look like then? Do you have to keep it as a plain JSON object or do you need to continue the rest of the object as a BadgerFish object?



    ------------------------------
    Sandile Ngubeni
    ------------------------------



  • 6.  RE: API Connect v10: Convert JSON request.body to XML with namespaces

    Posted Wed October 05, 2022 09:05 AM

    Hi Henri,

    XML output from a map policy is all based upon the output schema.  A simple way would be to use an inline schema for the XML output where you specify a sample XML with the desired namespaces for the elements that need namespaces.   The map policy will map the JSON and the schema provides the XML structure for the values to be output.

    Best Regards,
    Steve Linn



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 7.  RE: API Connect v10: Convert JSON request.body to XML with namespaces

    Posted Wed October 05, 2022 09:22 AM
    Hi Steve,

    thank you for your message. 
    This will be useful the next time I will be using JSON to XML if there are different namespaces for elements.

    Best regards,
    Henri Unruh

    ------------------------------
    Henri Unruh
    ------------------------------



  • 8.  RE: API Connect v10: Convert JSON request.body to XML with namespaces

    Posted Fri June 30, 2023 10:48 AM

    Hi Steve,

    Is there a sample project I can look at for this solution?



    ------------------------------
    Sandile Ngubeni
    ------------------------------



  • 9.  RE: API Connect v10: Convert JSON request.body to XML with namespaces

    Posted Tue July 11, 2023 06:01 PM

    Hi Sandile,

    I'm used to using the map policy.  Click on the pencil to create a input or output, and then choose the inline schema choice for the type.  You'll be presented a dialog where you can provide sample XML with a namespace, for example, 

    click on the generate button to get the actual schema 

    Best Regards,
    Steve Linn


    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 10.  RE: API Connect v10: Convert JSON request.body to XML with namespaces

    Posted Wed July 12, 2023 04:48 AM

    Thanks Steve, it worked.



    ------------------------------
    Regards,
    Sandile
    ------------------------------



  • 11.  RE: API Connect v10: Convert JSON request.body to XML with namespaces

    Posted Fri June 30, 2023 10:49 AM

    Hi Samir,

    So how does your JSON look like? How do you include the namespace in your JSON?



    ------------------------------
    Sandile Ngubeni
    ------------------------------



  • 12.  RE: API Connect v10: Convert JSON request.body to XML with namespaces

    Posted Thu September 29, 2022 11:48 AM
    I found using free online converters helpful, like freeformatter.com. It helped me find the right format of JSON, but I was using Maps

    ------------------------------
    Samir Almousawi
    ------------------------------