Forum Discussion
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
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.
Related Content
- 2 years ago
- 3 years ago
Recent Discussions
- 12 hours ago