Search code examples
jsongroovyjsonslurper

Groovy-JSONSlurper: Transform JSON map into a key=value pair


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?


Solution

  • 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)