2
1
0

Hi ,

I have a use case where I receive server output, from a Dynamic Data service call, in xml format and would like to convert it to JSON format.

Rather than hand craft a converter I was hoping to use a library and noticed that the Google Gson library is part of the 3rd Party Java Libraries included with TM.

However, I have not been able to find an example to use as a template to write the code.

The Gson documentation has examples of converting primitives and objects (an instance of a class). However, I have raw xml that does not have a defining class, so the xml is not 'strictly' a Java object.

Are there any resources available through Avoka documentation or other articles, or examples that I could use to get started?


Many Thanks

Mark

    CommentAdd your comment...

    4 answers

    1.  
      1
      0
      -1

      Hi All,

      thanks for the replies; lots of options to work with there.

      I did some more research last night and found some examples that use Groovy collect.

      I'll do some work on this and let you know what I end up with.

      Thanks again for all the feedback.


      Regards

      Mark

        CommentAdd your comment...
      1.  
        1
        0
        -1

        Hi Mark,

        If you need to transform the structure of the data while converting from XML to JSON, here is a sample from the documentation of the 17.10 release.  The bulk of the code will still work the same with earlier releases:

        FuncResult invoke(FuncParam param) {
            FormFuncResult result = new FormFuncResult()

            String message = param.svcDef.paramsMap.get('Soap Envelope')

            def response = new PostRequest(param.svcDef.svcConn.endpoint)
                .setContentType('text/xml')
                .setMessage(message).execute()

            if (!response.isStatusOK()) {
                throw new RuntimeException(response.statusLine)
            }

            def countries = new Path(response.getPathContent().val('string'))
                .paths('//Table[normalize-space(CurrencyCode) != ""]')

            result.data.put('success', true)
            result.data.put('countries', countries.collect {
                [
                    Name : it.val('//Name'),
                    CountryCode : it.val('//CountryCode').toString().toUpperCase(),
                    CurrencyCode: it.val('//CurrencyCode').toString().toUpperCase(),
                ]
            })

            return result

        This particular code pulls a list of country codes from http://www.webservicex.net/New/Home/ServiceDetail/17 and filters the list to remove entries with blank currency codes.

        Because Groovy services don't support GPath, the code makes use of the SDK Path object documented at Path, and Groovy collect. There is some helpful info about Groovy collect at http://mrhaki.blogspot.com.au/2011/09/groovy-goodness-collect-on-nested.html 

        Using Path in conjunction with collect allows you to write very tightly-coded and nicely readable transforms.

          CommentAdd your comment...
        1.  
          1
          0
          -1

          Hi Mark,

          Also have you considered using XmlSlurper?  We use it quite often and it's a good tool to help Xml parsing.  Search in Google for examples which may give you what you need.

          For instance:

          https://stackoverflow.com/questions/23374652/xml-to-json-with-groovy-xmlslurper-and-jsonbuilder

            CommentAdd your comment...
          1.  
            1
            0
            -1

            Hi Mark

            Have you tried something like this?

            Cheers
            Rado

            import net.sf.json.JSON
            import net.sf.json.xml.XMLSerializer
            
            
            XMLSerializer xmlSerializer = new XMLSerializer()
            JSON json = xmlSerializer.read(xmlString)
            json.toString(2)
              CommentAdd your comment...