Wednesday, November 27, 2024

Groovy export JSON to csv

Hello everyone, I continue to master the Groovy language, it is not easy of course, but the setting of tasks and a great desire oblige, today from interesting and curious tasks is exporting JSON to a csv file, I am very glad if I helped someone in solving it, the code is below:
def data = [
  ['id', 'Task', 'Name', 'Description'],
  ['1', 'Create', 'Jira', 'System Project'],
  ['2', 'Read', 'Confluence', 'Wiki'],
  ['3', 'Update', 'SharePoint', 'Portal']
]
				
def file = new File("C:/Temp/data.csv")
def stringBuilder = new StringBuilder()
 data.each { row ->
   stringBuilder.append(row.join(';')).append(System.lineSeparator())
}

file.text = stringBuilder.toString()
Result:

Happy Coding!

No comments:

Post a Comment