1

I have a JSON object, which we assume to be flat (no nesting) and a map. How can I transform this into a single string of key=value pairs, delimited by tab using JSONSlurper in Groovy?

oikonomiyaki
  • 7,691
  • 15
  • 62
  • 101

2 Answers2

2

For example like that:

StringBuilder keyStr = new StringBuilder()
def json = new JsonSlurper().parseText(jsonString)
json.each{keyStr.append(it.key).append("=").append(it.value).append("\t")}
someMap.put(strKey, someValue)
mzherdev
  • 462
  • 1
  • 4
  • 11
1
//parse json-string to map
def json = new groovy.json.JsonSlurper().parseText('{"a":"1","B":"22"}')
//convert map to array of `key=value` strings 
//and then join into one with new line delimiter
String txt = json.collect{"${it.key}=${it.value}"}.join('\n')
daggett
  • 26,404
  • 3
  • 40
  • 56