0

I am trying to write a bridge function to convert XML data to the Json format below are the data I have the sample xml file is

<testsuites>  <testsuite tests="4" failures="4" errors="0" name="AT">
   <testcase name="#1 notificate › v1 › announcement › announcement.feature/#TEST CASE: Notification: Send an announcement: Send an announcement using the minimum requirements"/>
   <testcase name="#2 notifiivate › v1 › announcement › announcement.feature/#TEST CASE: Notification: Send an ant"/>    
   <testcase name="#1 No tests found in features/tests/auth/auth.POST.js">
      <failure/>
    </testcase>
    <testcase name="#2 versioninfo › versioninfo › versioninfo.feature/#TEST CASE: CDP ADMIN: Get version info: Get the version of the CDP service">
      <failure>
            name: AssertionError
            message: Rejected promise returned by test
            values: 
      </failure>
    </testcase>
    <testcase name="#3 projects › edit_entitlement › edit_entitlement.feature/#TEST CASE: CDP ADMIN: Edit Entitlement: Attempt to edit an entitlement_id to be a negative number">
      <failure>
          ---
            name: AssertionError
            message: Rejected promise returned by test
            values: 
          ...
      </failure>
    </testcase>
  </testsuite>
</testsuites>

I am trying to write a function in groovy to get the below json format

{
testsuites{
    "testsuite": {
        "tests": "4",
        "failures": "4",
        "errors": "0",
        "name": "AT-cdpServer.Default",
        "testcase": [
        {
            "name": "#1 notificate › v1 › announcement › Send an announcement: Send an announcement using the minimum requirements"
        },
        {
            "name": "#2 notifiivate › v1 › announcement › announcement.feature/#TEST CASE: Notification: Send an ant"
        },
            {
                "name": "#1 No tests found in features/tests/auth/auth.POST.js",
                "failure": []
            },
            {
                "name": "#2 versioninfo › versioninfo › versioninfo.feature/#TEST CASE: CDP ADMIN: Get version info: Get the version of the CDP service",
                "failure": "---\n            name: AssertionError\n            message: Rejected promise returned by test\n            values: {\"Rejected promise returned by test. Reason:\":\"Error {\\n  message: 'no schema with key or ref \\\"/versioninfo.get.200\\\"',\\n}\"}\n            at: Ajv.validate (node_modules/ajv/lib/ajv.js:95:19)\n          ..."
            },
            {
                "name": "#3 projects › edit_entitlement › edit_entitlement.feature/#TEST CASE: CDP ADMIN: Edit Entitlement: Attempt to edit an entitlement_id to be a negative number",
                "failure": "---\n            name: AssertionError\n            message: Rejected promise returned by test\n            values: {\"Rejected promise returned by test. Reason:\":\"TypeError {\\n  message: 'Only absolute URLs are supported',\\n}\"}\n             ..."
            },
        ]
    }
}}
}

Appreciate any inputs in the right direction , thank you

So far I have this, it reads all the data, but the structure is off

def toJsonBuilder(xml){     
def xmlToJson = build(new XmlSlurper().parseText(xml)) 
new groovy.json.JsonBuilder(xmlToJson) 
} 

def build(node){
    if (node instanceof String){ 
        return // ignore strings...     
    }     
    def map = [(node.name()): node.collect]
    if (!node.attributes().isEmpty()) { 
         map.put(node.name(),node.attributes().collectEntries{it})
    }     
    if (!node.children().isEmpty() && !(node.children().getAt(0) instanceof String)) {  
        map.put(node.children().name, node.children().collect{build(it)}.findAll{it != null})     
    } else if (node.text() != ''){
        map.put(node.name(), node.text())     
    } 

    map 
  }
VKr
  • 3
  • 4
  • What have you tried so far? I would start with an XmlSlurper/XmlParser (depending on your dataset size and whether you're outputting all of it), read the data into a known structure, then use JsonOutput to create your output. – Daniel Mar 21 '19 at 23:20
  • @Daniel , I updated the post with the current work so far, I am not able to figure a way out to realign the children node part to get the desired result. Also, data is about 100s of nodes will be in the xml file, is it better to use XmlSlurper or the XmlParser? and I need to output it as a json object – VKr Mar 21 '19 at 23:33

0 Answers0