cancel
Showing results for 
Search instead for 
Did you mean: 

[Resolved] JSON conversion to XML does not work

Occasional Contributor

[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?
11 REPLIES 11

Re: JSON conversion to XML does not work

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


Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Occasional Contributor

Re: JSON conversion to XML does not work

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 );

Re: JSON conversion to XML does not work

Hi,

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



Regards,
Marcus
SmartBear Support


Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Contributor

Re: JSON conversion to XML does not work

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

Community Hero

Re: JSON conversion to XML does not work

Hi,

 

May I wonder why do you expect net.sf... objects to be imported? Are they in jars that are installed with ReadyAPI? Or have you provided ReadyAPI with the access to the relevant jar(s)?

Have you considered some alternative approach(es)?

For example:

https://stackoverflow.com/questions/29937254/convert-json-to-xml-using-groovy

http://groovy-lang.org/json.html (paragraph about "(XmlSlurper for XML being one example)") ?

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Contributor

Re: JSON conversion to XML does not work

Earlier i was using groovy-1.8.0 jar. I didnot get this issue. It would have been better if you have told what jars to be installed rather  posting random question to me.It would have helped me. Here issue is converting json to XML. not parsing json.Even i know based on the error message jar is missing. But dont know what i should install.Ready APi-Ready APi 2.8 coming with Groovy jar-2.4.4 jar. That  is having issue. Anyways  i figured out the solution. I installed the jars(ezmorph-1.0.1.jar &  json-lib-2.4-jdk15.jar). That will solve the problem. If any have any issues like me.

Community Hero

Re: JSON conversion to XML does not work

Hi,

 

> It would have been better if you have told what jars to be installed 

Have you asked for this?

Plain statement that something does not work means practically nothing because definition of 'work' (and error) is different for different people. And this may cause the one who is agree to spend his/her time to help you to ask additional questions to figure out what exactly does not work and what you are expecting to get.

 

> i was using groovy-1.8.0 jar

which does not seem to contain net.sf.json.* classes either.

 

> But dont know what i should install.

Third line of the search result for the https://www.google.com/search?q=jar+for+net.sf.json.JSON request returns a reference to https://mvnrepository.com/artifact/net.sf.json-lib/json-lib/2.2.3 which points to the json-lib-2.3.3.jar.

Quick look into the <ReadyAPI>\lib\ folder reveals that it contains the json-lib-X.X.X-jdkXX.jar and if you open this file with any zip-archive reader you can ensure that it contains net.sf.json.* classes that you were talking about.

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Highlighted
Contributor

Re: JSON conversion to XML does not work

I dont want to waste someone's time .If i know what is the issue. i would not post the problem . After spending sometime. I figureout jar issue.Based on the information provided i have verified Ready API 2.1.0  has json-lib-2.2.2-jdk15-fixed.jar. where as coming to Ready APi 2.8.2 i dont see any jar in the lib folder.Right now its clear to me based on your comments where to look in future.Thank you!

Community Hero

Re: JSON conversion to XML does not work

Hi,

 

> Ready API 2.1.0  has json-lib-2.2.2-jdk15-fixed.jar. where as coming to Ready APi 2.8.2 i dont see any jar in the lib folder.

Hm-m-m... Sounds strange. I don't know for ReadyAPI 2.8.2, but ReadyAPI 3.0 contains json-lib-2.2.2-jdk15.jar.

The problem is that it is not possible to figure out what jar file is missed when some class is not found.

https://stackoverflow.com/questions/29624501/identify-missing-jars-from-exception-type-java explained the problem to me quite well.

(My knowledge of Java/Groovy is very limited, so I don't know what code will be picked-up if more than one jar file contains the code for the class with the same name.)

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================