Forum Discussion

UPC_Broadband_L's avatar
UPC_Broadband_L
Occasional Contributor
11 years ago

[Resolved] JSON conversion to XML does not work

Hi,

Right now, I'm using a REST api for a Jira plugin, Zephyr (here's the documentation for reference [though not necessary for this issue]: http://docs.getzephyr.apiary.io/).

They all return JSON responses. SoapUI seems to automatically convert these to XML so they can be edited / processed.
However, one of them returns this:

{
"status": {
"1": {
"id": 1,
"color": "#75B000",
"description": "Test was executed and passed successfully.",
"name": "PASSED"
},
"2": {
"id": 2,
"color": "#CC3300",
"description": "Test was executed and failed.",
"name": "FAILED"
},
"3": {
"id": 3,
"color": "#ffff00",
"description": "Test is Not tested in this cycle.",
"name": "NOT TESTED"
},
"4": {
"id": 4,
"color": "#6693B0",
"description": "The test execution of this test was suspended.",
"name": "TEST SUSPENDED"
},
"6": {
"id": 6,
"color": "#ff9900",
"description": "Test is failed, but not a high priority issue",
"name": "ACCEPTED FAILED"
},
"-1": {
"id": -1,
"color": "#bbbbbb",
"description": "The test has not yet been executed.",
"name": "SCHEDULED"
}
},
"issueId": 1234,
"executions": [ {
"id": 1234,
"executionStatus": "-1",
"comment": "",
"htmlComment": "",
"cycleId": 1234,
"cycleName": "Cycle Name 2014-06-05",
"versionId": 1234,
"versionName": "version",
"projectId": 1234,
"issueId": 1234,
"issueKey": "TEST-1234",
"summary": "Summary Sample 1",
"issueDescription": "<p>&lt;p&gt;&lt;span style=\"color: rgb(51, 51, 51); font-family: Arial, sans-serif; font-size: 14px; line-height: 20px;\"&gt;test &amp; test.&lt;/span&gt;&lt;/p&gt;<\/p>",
"label": "TEST, TEST2",
"component": "TEST",
"projectKey": "TEST"
}],
"currentlySelectedExecutionId": "",
"recordsCount": 1
}


Apparently soapUI's automatic JSON to XML conversion fails.
It returns this: <xml/>

Ergo, I can't manipulate it, transfer properties, etc.
Trying to use a groovy script to retrieve only the JSON as a string always returns the "<xml/>" response. Ex:
- def response = context.expand( '${name#Response}' ).toString()
- responseContent = testRunner.testCase.getTestStepByName("name").getPropertyValue("response")

ResponseAsXml does not work in this case, since the XML was not properly converted.
- Note: The error returned is "NCNames cannot start with the character 2d", probably because one of the elements starts with a number and that does not follow XML conventions.

Is there a way to:
1- Fix the response so it's "XML convertible" JSON?
or
2- Fetch the response as JSON only, not wrongly converted XML, so that it can be slurped?
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    The problem is converting the numbers for the JSON object names "1","2", "3", "4","6", and "-1" to XML. Element names can not be numbers in XML. That will result in the error you are seeing. As a workaround you can change the object names before they are converted with the event handler RequestFilter.afterRequest and code below.


    context.httpResponse.responseContent = context.httpResponse.responseContent.replace("\"1\"","\"a\"")
    context.httpResponse.responseContent = context.httpResponse.responseContent.replace("\"2\"","\"b\"")
    context.httpResponse.responseContent = context.httpResponse.responseContent.replace("\"3\"","\"c\"")
    context.httpResponse.responseContent = context.httpResponse.responseContent.replace("\"4\"","\"d\"")
    context.httpResponse.responseContent = context.httpResponse.responseContent.replace("\"6\"","\"e\"")
    context.httpResponse.responseContent = context.httpResponse.responseContent.replace("\"-1\"","\"f\"")
    log.info context.httpResponse.responseContent


    http://www.soapui.org/Scripting-Propert ... dlers.html

    Development is aware of the issues with converting XML to JSON and these should be addressed in future releases of SoapUI Pro.

    Regards,
    Marcus
    SmartBear Support
  • UPC_Broadband_L's avatar
    UPC_Broadband_L
    Occasional Contributor
    Thanks for the reply. That does work.

    I also managed to work around it (without replacing it) by using the following to transfer variables I wanted:

    import groovy.json.JsonSlurper

    def response = context.expand( '${name#Response}' )

    log.info "response: "+response

    def slurper = new JsonSlurper()
    def json = slurper.parseText response

    def String stringExecutionId = json.executions.id[0]
    log.info "ExecutionId: "+stringExecutionId

    testRunner.testCase.testSteps["Properties"].setPropertyValue( "executionId", stringExecutionId );
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    I am glad you posted an alternate solution to this problem. I will mark this post as resolved now.



    Regards,
    Marcus
    SmartBear Support
    • harry's avatar
      harry
      Contributor

      I dont see here he is converting json to xml. He still parsing using Jsonsulpher.

      //Conversion from json to xml
      def jsontoxml;
      XMLSerializer xmlSerializer = new XMLSerializer();
      JSON json = JSONSerializer.toJSON(parsed_json);
      xmlSerializer.setTypeHintsEnabled(false);
      jsontoxml = xmlSerializer.write(json);
      log.info "JSON Response converted to XML Response Result:" + jsontoxml;

      currenlty i have in code where Ready 2.1.0 works. but now when i try to upgrade  Ready 2.8.2 i was getting error.

       

      import net.sf.json.JSON
      import net.sf.json.JSONSerializer
      import net.sf.json.xml.XMLSerializer

       

      are not working