<?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: Preparing nested JSON using SQL in NiFi in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/Preparing-nested-JSON-using-SQL-in-NiFi/m-p/381416#M244306</link>
    <description>&lt;P&gt;...a 3rd option because I like scripted processors &lt;span class="lia-unicode-emoji" title=":face_with_tears_of_joy:"&gt;😂&lt;/span&gt;...using ExcecuteGroovyScript&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import java.nio.charset.StandardCharsets

JsonSlurper jsonSlurper = new JsonSlurper()
JsonOutput jsonOutput = new JsonOutput()

FlowFile flowFile = session.get()
if(!flowFile) return

flowFile = session.write(flowFile, { inputStream, outputStream -&amp;gt; 
                                        List&amp;lt;Map&amp;gt; data = jsonSlurper.parse(inputStream)
                                        data.each { 
                                            it.order_item = jsonSlurper.parseText(it.order_item)
                                        }
                                        outputStream.write(jsonOutput.toJson(data).getBytes(StandardCharsets.UTF_8))
                                  } as StreamCallback)
session.transfer(flowFile, REL_SUCCESS)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looks like a lot but this is what takes the string JSON and converts it to JSON:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;it&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;order_item &lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt; jsonSlurper&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;parseText(it&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;order_item)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Thu, 28 Dec 2023 20:26:34 GMT</pubDate>
    <dc:creator>joseomjr</dc:creator>
    <dc:date>2023-12-28T20:26:34Z</dc:date>
    <item>
      <title>Preparing nested JSON using SQL in NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Preparing-nested-JSON-using-SQL-in-NiFi/m-p/381363#M244292</link>
      <description>&lt;P&gt;Hey there,&lt;/P&gt;&lt;P&gt;I'm trying to create a nested JSON using SQL in NiFi, but the output I'm getting has the nested part as a string. Here's the query I'm using:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;SELECT
order_id,
JSON_ARRAYAGG(
JSON_OBJECT(
'order_Item_Seq_Id', order_Item_Seq_Id,
'product_Id', product_Id
)
) as order_item
FROM order_item
GROUP BY order_id;&lt;/LI-CODE&gt;&lt;P&gt;The output looks like this:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[{
"order_id": "10000",
"order_item": "[{\"product_Id\": \"10007\", \"order_Item_Seq_Id\": \"00101\"}]"
}]&lt;/LI-CODE&gt;&lt;P&gt;But I want it to be a valid nested JSON like this:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[
{
"order_id": "10000",
"order_item": [
{
"product_Id": "10007",
"order_Item_Seq_Id": "00101"
}
]
}
]&lt;/LI-CODE&gt;&lt;P&gt;I'm hoping for a solution using Apache NiFi or Jolt.&lt;BR /&gt;Any help would be appreciated.&lt;BR /&gt;&lt;BR /&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 28 Dec 2023 06:15:41 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Preparing-nested-JSON-using-SQL-in-NiFi/m-p/381363#M244292</guid>
      <dc:creator>Sadhana21</dc:creator>
      <dc:date>2023-12-28T06:15:41Z</dc:date>
    </item>
    <item>
      <title>Re: Preparing nested JSON using SQL in NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Preparing-nested-JSON-using-SQL-in-NiFi/m-p/381404#M244300</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/108507"&gt;@Sadhana21&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/105558"&gt;@joseomjr&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/103151"&gt;@cotopaul&lt;/a&gt;&amp;nbsp;&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>Thu, 28 Dec 2023 15:24:02 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Preparing-nested-JSON-using-SQL-in-NiFi/m-p/381404#M244300</guid>
      <dc:creator>DianaTorres</dc:creator>
      <dc:date>2023-12-28T15:24:02Z</dc:date>
    </item>
    <item>
      <title>Re: Preparing nested JSON using SQL in NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Preparing-nested-JSON-using-SQL-in-NiFi/m-p/381414#M244304</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/108507"&gt;@Sadhana21&lt;/a&gt; ,&lt;/P&gt;&lt;P&gt;You can do this in two ways that I can think of :&lt;/P&gt;&lt;P&gt;1-&amp;nbsp; This might take few processors but basically after you get the output:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[{
"order_id": "10000",
"order_item": "[{\"product_Id\": \"10007\", \"order_Item_Seq_Id\": \"00101\"}]"
}]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1.1&amp;nbsp; First EvaluateJsonPath to get the&amp;nbsp; order_id and set the destination to attribute:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAMSAL_0-1703791815125.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/39299i99B6A8A9669E5C02/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SAMSAL_0-1703791815125.png" alt="SAMSAL_0-1703791815125.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1.2&amp;nbsp; 2ed EvaluateJsonPath to get the order_item and set the destination to flowfile and the Return Type as Json&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAMSAL_1-1703791867077.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/39300iCA26125E22825CAC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SAMSAL_1-1703791867077.png" alt="SAMSAL_1-1703791867077.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; This will produce the following output:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[{"product_Id": "10007", "order_Item_Seq_Id": "00101"}]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1.3 Finally you JoltTransformJson with the following spec:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "#${order_id}": "[&amp;amp;1].order_id",
        "@": "[&amp;amp;1].order_item[]"
      }
    }
  }
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; This should give you the needed result&lt;/P&gt;&lt;P&gt;2- Using just UpdateRecord Processor:&lt;/P&gt;&lt;P&gt;I like this option because that is the only processor you need to use. The only caveat is that you need to define JsonRecordReader &amp;amp; JsonRecordSetWriter where in the later you have to define Avro schema for the expected output. In the UpdateRecord you can use the built-in function called "unescapeJson" to read json string and return it as json:&lt;/P&gt;&lt;P&gt;&lt;A href="https://nifi.apache.org/docs/nifi-docs/html/record-path-guide.html#unescapejson" target="_blank"&gt;https://nifi.apache.org/docs/nifi-docs/html/record-path-guide.html#unescapejson&lt;/A&gt;&lt;/P&gt;&lt;P&gt;UpdateRecord:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAMSAL_2-1703792282831.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/39301i502FF7408844DA92/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SAMSAL_2-1703792282831.png" alt="SAMSAL_2-1703792282831.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; JsonRecordSetWriter:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAMSAL_3-1703792337746.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/39302iC043D6B3D0832124/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SAMSAL_3-1703792337746.png" alt="SAMSAL_3-1703792337746.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; Avro Scheam in the Schema Text:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{
	"name": "nifi",
	"type": "record",
	"fields": [
		{
			"name": "order_id",
			"type": "string"
		},
		{
			"name": "order_item",
			"type": {
				"name": "order_item_array",
				"type": "array",
				"items": {
					"name": "order_item",
					"type": "record",
					"fields": [
						{
							"name": "product_Id",
							"type": "string"
						},
						{
							"name": "order_Item_Seq_Id",
							"type": "string"
						}
					]
				}
			}
		}
	]
}&lt;/LI-CODE&gt;&lt;P&gt;If that helps 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>Thu, 28 Dec 2023 19:40:50 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Preparing-nested-JSON-using-SQL-in-NiFi/m-p/381414#M244304</guid>
      <dc:creator>SAMSAL</dc:creator>
      <dc:date>2023-12-28T19:40:50Z</dc:date>
    </item>
    <item>
      <title>Re: Preparing nested JSON using SQL in NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Preparing-nested-JSON-using-SQL-in-NiFi/m-p/381416#M244306</link>
      <description>&lt;P&gt;...a 3rd option because I like scripted processors &lt;span class="lia-unicode-emoji" title=":face_with_tears_of_joy:"&gt;😂&lt;/span&gt;...using ExcecuteGroovyScript&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import java.nio.charset.StandardCharsets

JsonSlurper jsonSlurper = new JsonSlurper()
JsonOutput jsonOutput = new JsonOutput()

FlowFile flowFile = session.get()
if(!flowFile) return

flowFile = session.write(flowFile, { inputStream, outputStream -&amp;gt; 
                                        List&amp;lt;Map&amp;gt; data = jsonSlurper.parse(inputStream)
                                        data.each { 
                                            it.order_item = jsonSlurper.parseText(it.order_item)
                                        }
                                        outputStream.write(jsonOutput.toJson(data).getBytes(StandardCharsets.UTF_8))
                                  } as StreamCallback)
session.transfer(flowFile, REL_SUCCESS)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looks like a lot but this is what takes the string JSON and converts it to JSON:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;it&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;order_item &lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt; jsonSlurper&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;parseText(it&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;order_item)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 28 Dec 2023 20:26:34 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Preparing-nested-JSON-using-SQL-in-NiFi/m-p/381416#M244306</guid>
      <dc:creator>joseomjr</dc:creator>
      <dc:date>2023-12-28T20:26:34Z</dc:date>
    </item>
    <item>
      <title>Re: Preparing nested JSON using SQL in NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Preparing-nested-JSON-using-SQL-in-NiFi/m-p/381540#M244332</link>
      <description>&lt;P&gt;Your input means a lot, and I appreciate your assistance&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/93628"&gt;@DianaTorres&lt;/a&gt;,&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/80381"&gt;@SAMSAL&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/105558"&gt;@joseomjr&lt;/a&gt;.&lt;/P&gt;&lt;P&gt;Thank you all for sharing your knowledge and insights!&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jan 2024 05:32:10 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Preparing-nested-JSON-using-SQL-in-NiFi/m-p/381540#M244332</guid>
      <dc:creator>Sadhana21</dc:creator>
      <dc:date>2024-01-02T05:32:10Z</dc:date>
    </item>
  </channel>
</rss>

