Cant write json output to a XML file
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Cant write json output to a XML file
Below is my code to specifically get values at
result._source.city
node.
import net.sf.*; import net.sf.json.*; import net.sf.json.groovy.*; import net.sf.json.groovy.JsonSlurper; def ResFile ="C:/driver/Response1.xml" def Res = context.expand( '${GetAllSchoolsForNZ#Response}' ) def slurper = new JsonSlurper() def json = slurper.parseText Res def countryList = json.result._source.city log.info countryList def j = new File(ResFile) j.write(countryList, "UTF-8",true)
But when i execute it im gettint an error like this.
No signature of method: java.io.File.write() is applicable for argument types: (java.util.ArrayList, java.lang.String, java.lang.Boolean) values: [[Rotorua, Christchurch, Auckland, Auckland, ...], ...] Possible solutions: write(java.lang.String, java.lang.String), write(java.lang.String), wait(), size(), canWrite(), delete()
So how am i going to write my data in to a xml file? or any other file?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I manage to write json in to a file using below code.
import net.sf.*; import net.sf.json.*; import net.sf.json.groovy.*; import net.sf.json.groovy.JsonSlurper; def Res = context.expand( '${GetAllSchoolsForAustralia#Response}' ) def slurper = new JsonSlurper() def json = slurper.parseText Res def countryList = json.result._source.city log.info countryList def file = new File('D:/XXX/CityListAU.txt'); // save the response to it file << countryList
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi @chathurad,
Thanks for sharing the solution!
Please note that you can mark your own replies as Solutions - this will help other users find the answers easier.
Have a great day,
Olga Terentieva
SmartBear Assistant Community Manager
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I hope you don't mind but your question and answer piqued my interest to why one would work but not the other, so I investigated this a bit further. So, just incase anyone else comes across this post, I believe the reason the .write method fails but the .leftShift (the "<<" operator) succeeds is that the Json Slurper returns lists or maps of objects. Thus the variable "countryList" in the above examples is a Groovy List.
You can see from the Groovy File Class Java Docs that the two methods signatures are:
write(String text, String charset)
&
leftShift(Object text)
Thus you can see how you can pass a List to leftShift (Where I assume that the toString() method is called), but not to the write method which explicitly expects a string.
Hope this might help somebody.
