[Resolved] JSON conversion to XML does not work
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[Resolved] JSON conversion to XML does not work
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><p><span style=\"color: rgb(51, 51, 51); font-family: Arial, sans-serif; font-size: 14px; line-height: 20px;\">test & test.</span></p><\/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?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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. ⬇️⬇️⬇️
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 );
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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. ⬇️⬇️⬇️
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)") ?
/Alex [Community Champion]
____
[Community Champions] 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. Posts made by [Community Champions]
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.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
/Alex [Community Champion]
____
[Community Champions] 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. Posts made by [Community Champions]
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.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.)
/Alex [Community Champion]
____
[Community Champions] 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. Posts made by [Community Champions]
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.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
