Layer7 API Management

  • 1.  How to simply convert XML into JSON, validating JSON Schema ?

    Posted Aug 02, 2016 05:03 AM

    Hi dear CA community,

     

    I have recurrent problems when converting XML into JSON. Indeed, I often get a JSON Schema of the response from my consumers, and hit an XML backend. Obviously, I have a transformation step to make.

    Here is my problem:

    - using "Apply JSON transformation" assertion, I am not able to enforce the "type" of my node (mainly "array", "string" and "integer")

    - using "Apply XSL Transformation" assertion, I am able to have the correct type in output as I am the master of the syntax, manually adding "[" or "{" for example. But this process is quite heavy and can be a source of error.

     

    Do you guys know a simple way to generate an XSL file, that validates a JSON Schema in output? How are you processing this kind of transformation?

     

    Thank you for your feedback!



  • 2.  Re: How to simply convert XML into JSON, validating JSON Schema ?

    Broadcom Employee
    Posted Aug 04, 2016 08:58 AM

    Very interesting question martial.boyer,

     

    I also faced similar annoying "handmade" post-transformation processing to do when initial XML payload contains array with 0 or only 1 item. In JSON equivalent, array types disappears for object

    I'm currently using regex (with search and replace conf) or/and xsl operation, but it is not a generic implementation.

     

    Any idea dear CA APIM users ?

    cc FrederickMiszewski chovine potbe01



  • 3.  Re: How to simply convert XML into JSON, validating JSON Schema ?

    Posted Aug 30, 2016 05:53 AM

    Hi Martial and Nicolas, have you find out a generic response to your issue ? I have the same defect with the Apply  json transformation. CA support send an example with a xls operation. But this workaround do not works with nested arrays or elements. kind regard



  • 4.  Re: How to simply convert XML into JSON, validating JSON Schema ?

    Posted Aug 30, 2016 10:43 AM

    Hi,

    Today, XSLT seems to be the very most appropriate and powerful solution.
    It seems to be also possible to enforce the creation of "false" nodes in the XML when you want to display an array, then remove the specific "false" keys in your JSON, using a Reg Exp.

     

     

     

    XMLJSON

    <youWantAnArray>

       <array false1></array>

       <array>real value</array>

       <array false2></array>

    </youWantAnArray>

    {
       "youWantAnArray": {
          "array": [{
            "false1": ""
        }, "real value", {
            "false2": ""
        }]
       }
    }

    Hope this helps,

    Martial



  • 5.  Re: How to simply convert XML into JSON, validating JSON Schema ?

    Posted Aug 31, 2016 04:16 AM

    thanks Martial.

     

    We have implemented another way :

    -> lookup node expected in JSON Message (Evaluate JSON Path Expression)

    -> check if it contains singleElement ou multiElement (Compare Variable "count" <= 1)

    -> if singleElement, replace the node expected with a node type array like this :  

                          "nodeexpected":{...} ==> "nodeexpected": [{...}]

     

    and we loop in this way on each node identified.

     

    Later, we will try to experiment the power of this method and the one you proposed.

    best regards
     

    .



  • 6.  Re: How to simply convert XML into JSON, validating JSON Schema ?

    Broadcom Employee
    Posted Oct 02, 2018 01:30 AM

    I did try this for one customer - both methods above would seem doable for a simple transformation - but anything more complex would take some time and be quite complex.

     

    You need a list of the XPath or JSON paths of those elements you expect to return arrays. 

     

    Approaches : 

     

    1) Check them in XML, Use XPAth to find all the paths you want as lists but that only one element. 

        Then use "Add Or Remove XML Element"  to add a "dummy" extra node at that point. 

        Then do the XML -> JSON conversion

         then remove all the "dummy" entries via RegEx assertion. 

     

    2) After done XML->JSON, then use JSON Path to identify the single nodes, and then directly use regex to manipulate 
                       "nodeexpected":{...} ==> "nodeexpected": [{...}]

     

    3) Write it in java as a custom assertion. 

     

    Notes: a) Need to be careful don't remove real data in the regex - since scope is whole document not just sub part.

               b) regex is more difficult to find end delimiter with linefeeds , so used non-formatted xml->json transform. 

               c) An  "Add Or Remove JSON Element" assertion would have been useful - but does not exist.

     

    In the sample I was working with, there were nested cases of elements that needed to be arrays, so that would probably mean applying these techniques in a recursive encapsulated assertion, probably best if that walked through the subtree handling each child subtree and returning the converted children -  would be needed be fairly tricky. 

     

    Cheers - Mark



  • 7.  Re: How to simply convert XML into JSON, validating JSON Schema ?

    Posted Nov 24, 2018 10:19 AM

    Thanks to Gateway v9.4, we have now Javascript custom assertion. Makes life much easier!!

     

    Another option without any coding that takes JSON types into account via a custom assertion I have developed. It relies on the XML hint included in the OpenAPI 3.0 json object.

     

    {
      "$id": "https://example.com/person.schema.json",
      "$schema": "http://json-schema.org/draft-07/schema#",
      "title": "Very Simple Object",
      "type": "object",
      "properties": {
        "aString": {
          "type": "string",
          "xml": {
            "name": "xml_string"
          }
        },
        "aNumber": {
          "type": "number"
        }
      }
    }

     

    Hope this helps. Feel free to request enhancements or raise issues!

    GitHub - guyplusplus/JSON-XML-Converter-Custom-Assertion: This CA API Gateway (Layer 7) custom assertion converts XML / …