1

I'm struggling with getting a groovy script to output what I need adhering to a vendor spec. Eventually, I will populate JsonSlurper with a file rather than static rows, but for simplicity I've provided sample JSON. When running the following code in GroovyConsole, I can't seem to pinpoint why the UserReference and Bal nodes are including the namespace I specify. Any help is appreciated!

    import groovy.xml.* 
    import groovy.json.JsonSlurper  
    def queryResultsResponseString =  
    ''' 
    { 
    "rows": [ 
    { 

                     "account_num": 123, 
                     "name": "Mr Bigbucks", 
                     "balance": 83.23 
                   }, 
                   { 
                     "account_num": 8675309, 
                     "name": "Johnny", 
                     "balance": 45.75 
                   } 
                  ] 
       } 
     ''' 
     def jsonResp = (new JsonSlurper()).parseText(queryResultsResponseString); 
     def xml = new groovy.xml.StreamingMarkupBuilder()
     xml.encoding = "UTF-8"
     def prints = xml.bind{ 
        mkp.xmlDeclaration()  
      mkp.declareNamespace(p1 :"com/my/namespace/woot" + "\"" + " " + "xsi:schemaLocation=\"http://www.w3.org/2001/XMLSchema-instance") 

      'p1:BatchEidvPersonSearch' { 
       jsonResp.rows.each{row -> 
         EidvPersonSearchRequest { 
           InternalId('Internal Id')
           UserReference('CLEAR ID Confirm Person Search')
           criteria {
           acctNum row.account_num 
           custName row.name 
           bal row.balance 
           }
         } 
       } 
     } 
     }
    println XmlUtil.serialize(prints).replaceAll('"','"')
// ---Results Here ---
    <?xml version="1.0" encoding="UTF-8"?><p1:BatchEidvPersonSearch xmlns:p1="com/my/namespace/woot" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
      <EidvPersonSearchRequest>
        <InternalId>Internal Id</InternalId>
        <UserReference xmlns:p1="com/my/namespace/woot" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">CLEAR ID Confirm Person Search</UserReference>
        <criteria>
          <acctNum>123</acctNum>
          <custName>Mr Bigbucks</custName>
          <bal xmlns:p1="com/my/namespace/woot" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">83.23</bal>
        </criteria>
      </EidvPersonSearchRequest>
      <EidvPersonSearchRequest>
        <InternalId>Internal Id</InternalId>
        <UserReference xmlns:p1="com/my/namespace/woot" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">CLEAR ID Confirm Person Search</UserReference>
        <criteria>
          <acctNum>8675309</acctNum>
          <custName>Johnny</custName>
          <bal xmlns:p1="com/my/namespace/woot" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">45.75</bal>
        </criteria>
      </EidvPersonSearchRequest>
    </p1:BatchEidvPersonSearch>

--- Update -----

I am attempting to invoke this Groovy script through an ETL tool that comes with the 2.2 Groovy Jar loaded locally and noticed that when invoking the script through the ETL tool, it produces the output I expect. So...I downloaded 2.2 groovy from the archives and invoke my script using groovy console and BAM it works. I'm not sure what changed between versions, but there's definitely a change in output. I suspect that when we upgrade our ETL tool, the JAR will be refreshed with the later-est version. Any ideas what might be significantly different between the two releases?

  • 1
    When I run your code exactly as provided on http://groovyconsole.appspot.com/ I get the expected result, not what you're seeing. What version of groovy are you using, and are there any additional customizations not shown in your question? – Daniel Feb 26 '19 at 16:36
  • Funny, I began the development using that exact site and it was outputting what I expected. I'm using Groovy 2.5.6. With respect to the "additional customization" question, I don't think so, but what are you specifically asking? – Ricky Danzberger Feb 26 '19 at 17:36
  • It's possible to register custom serializers or additionally customize your output through other convoluted means. You would probably know if you're doing that. Otherwise all I can think of is that maybe you have some other `` and `` object in scope somehow so the serializer is adding the namespace to clarify which one it means. You may also try putting quotes around bal and UserReference in your .each closure; I have occasionally seen odd behavior when you use a key that already exists in scope as a variable name (doesn't look like it here but maybe in your full code?) – Daniel Feb 26 '19 at 23:57

0 Answers0