Created 02-12-2024 03:02 PM
Hello,
I have nested JSON objects coming in a flowfile, looking like this (example):
Created 02-13-2024 09:05 AM
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
Created 02-12-2024 04:20 PM
@kekotron Welcome to the Cloudera Community!
To help you get the best possible solution, I have tagged our NiFi experts @MattWho @joseomjr who may be able to assist you further.
Please keep us updated on your post, and we hope you find a satisfactory solution to your query.
Regards,
Diana Torres,Created 02-13-2024 09:05 AM
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
Created 02-13-2024 03:09 PM
Thanks! This scripting approach looks to be working. I'll be sure to look into the ExecuteScript processor more 🙂