<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: Replacing characters within a nested json in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/Replacing-characters-within-a-nested-json/m-p/383460#M244914</link>
    <description>&lt;P&gt;Thanks! This scripting approach looks to be working. I'll be sure to look into the ExecuteScript processor more &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 13 Feb 2024 23:09:51 GMT</pubDate>
    <dc:creator>kekotron</dc:creator>
    <dc:date>2024-02-13T23:09:51Z</dc:date>
    <item>
      <title>Replacing characters within a nested json</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Replacing-characters-within-a-nested-json/m-p/383431#M244901</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have nested JSON objects coming in a flowfile, looking like this (example):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="content text-message-content"&gt;&lt;DIV&gt;{ "a" : 1, "b" : {"asd" : 156, "bed" : "lalala"}, "c" : "abba", "d" : {"add" : 872, "bla" : "bebebe"} }&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I'd like to put double quotes around every nested json object and also escape any double quote inside of that nested json. The result would look like this:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;{ "a" : 1, "b" : "{\"asd\" : 156, \"bed\" : \"lalala\"}", "c" : "abba", "d" : "{\"add\" : 872, \"bla\" : \"bebebe\"}" }&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I tried doing a replacetext with a regex like this: (?&amp;lt;=:.*\{[^}]*)"&lt;BR /&gt;However this is utilizing a negative lookbehind with varying length and it seems this isn't supported in java/nifi. What would be the best approach here? Thanks!&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 12 Feb 2024 23:02:26 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Replacing-characters-within-a-nested-json/m-p/383431#M244901</guid>
      <dc:creator>kekotron</dc:creator>
      <dc:date>2024-02-12T23:02:26Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing characters within a nested json</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Replacing-characters-within-a-nested-json/m-p/383432#M244902</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/109158"&gt;@kekotron&lt;/a&gt;&amp;nbsp;Welcome to the Cloudera Community!&lt;BR /&gt;&lt;BR /&gt;To help you get the best possible solution, I have tagged our NiFi experts&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/35454"&gt;@MattWho&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/105558"&gt;@joseomjr&lt;/a&gt;&amp;nbsp; who may be able to assist you further.&lt;BR /&gt;&lt;BR /&gt;Please keep us updated on your post, and we hope you find a satisfactory solution to your query.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Feb 2024 00:20:18 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Replacing-characters-within-a-nested-json/m-p/383432#M244902</guid>
      <dc:creator>DianaTorres</dc:creator>
      <dc:date>2024-02-13T00:20:18Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing characters within a nested json</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Replacing-characters-within-a-nested-json/m-p/383454#M244912</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/109158"&gt;@kekotron&lt;/a&gt; ,&lt;/P&gt;&lt;P&gt;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&amp;nbsp; 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.&lt;/P&gt;&lt;P&gt;The ExecuteScript below is written using groovy but you can do the same with other languages as well.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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 -&amp;gt;
  jsonText = IOUtils.toString(inputStream, StandardCharsets.UTF_8)

  jsonMap = new JsonSlurper().parseText(jsonText)

  jsonMap.each{k,v-&amp;gt;
    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)&lt;/LI-CODE&gt;&lt;P&gt;For more info on how to write write script inside ExecuteScript:&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-2/ta-p/249018" target="_blank"&gt;https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-2/ta-p/249018&lt;/A&gt;&lt;/P&gt;&lt;P&gt;If you find this helpful please &lt;STRONG&gt;accept&lt;/STRONG&gt; solution.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Feb 2024 17:05:04 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Replacing-characters-within-a-nested-json/m-p/383454#M244912</guid>
      <dc:creator>SAMSAL</dc:creator>
      <dc:date>2024-02-13T17:05:04Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing characters within a nested json</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Replacing-characters-within-a-nested-json/m-p/383460#M244914</link>
      <description>&lt;P&gt;Thanks! This scripting approach looks to be working. I'll be sure to look into the ExecuteScript processor more &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Feb 2024 23:09:51 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Replacing-characters-within-a-nested-json/m-p/383460#M244914</guid>
      <dc:creator>kekotron</dc:creator>
      <dc:date>2024-02-13T23:09:51Z</dc:date>
    </item>
  </channel>
</rss>

