Support Questions

Find answers, ask questions, and share your expertise

Who agreed with this solution

avatar
Super Guru

Hi @kekotron ,

There is no simple out of the box solution for this that I can think of. The easiest way is to use ExecuteScript processor that parse the json  as a map, then loop through each key and check if the value of that key is of type map as well- which means nested json - to then convert the map to json string and re assign back to the same key.

The ExecuteScript below is written using groovy but you can do the same with other languages as well.

import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets
import groovy.json.JsonOutput
import groovy.json.JsonSlurper

flowFile = session.get()
if(!flowFile) return

// Cast a closure with an inputStream and outputStream parameter to StreamCallback
flowFile = session.write(flowFile, {inputStream, outputStream ->
  jsonText = IOUtils.toString(inputStream, StandardCharsets.UTF_8)

  jsonMap = new JsonSlurper().parseText(jsonText)

  jsonMap.each{k,v->
    if(jsonMap[k] instanceof Map)
       jsonMap[k] = JsonOutput.toJson(jsonMap[k])

    }

  outputStream.write(JsonOutput.toJson(jsonMap).getBytes(StandardCharsets.UTF_8))
} as StreamCallback)
session.transfer(flowFile, REL_SUCCESS)

For more info on how to write write script inside ExecuteScript:

https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-2/ta-p/249018

If you find this helpful please accept solution.

Thanks

 

View solution in original post

Who agreed with this solution