<?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: Create a json payload using a template and its values in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/Create-a-json-payload-using-a-template-and-its-values/m-p/390809#M247342</link>
    <description>&lt;P&gt;You could implement this using Groovy script which is forward and backward compatible. I've taken code written in NiFi 1.X and works just fine in 2.X&lt;/P&gt;</description>
    <pubDate>Mon, 22 Jul 2024 19:44:24 GMT</pubDate>
    <dc:creator>joseomjr</dc:creator>
    <dc:date>2024-07-22T19:44:24Z</dc:date>
    <item>
      <title>Create a json payload using a template and its values</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Create-a-json-payload-using-a-template-and-its-values/m-p/390631#M247315</link>
      <description>&lt;P&gt;I have a python script that takes in text input in the format like this&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;{"order":{"Testorder":{"operation":"create","specification":{"name":"${name}","version":"1.0.0-SNAPSHOT"},"parameters":{"controllerName":"${controllername}","parameters":{"parameter1":"${value1}","parameter2":"${value2}"}}}}}
{"name": "TestService", "controllername": "MyController_d","value1": "test","value2": "speed"}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;First line is a JSON Template with place holders. Second line another JSON string which holds the value to be replaced in the first line. (whole input (first line and second line together) are not a valid JSON)&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="scoutjohn_0-1721398803934.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/41196i63F51891EDD2764B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="scoutjohn_0-1721398803934.png" alt="scoutjohn_0-1721398803934.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;It creates an output like this&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;{
  "order" : {
    "Testorder" : {
      "operation" : "create",
      "specification" : {
        "name" : "TestService",
        "version" : "1.0.2-SNAPSHOT"
      },
      "parameters" : {
        "controllerName" : "MyController_d",
        "parameters" : {
          "parameter1" : "test",
          "parameter2" : "strength"
        }
      }
    }
  }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have implemented this using a python script inside &lt;STRONG&gt;ExecuteScript&lt;/STRONG&gt; processor&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import json
import sys
import traceback
from java.nio.charset import StandardCharsets
from org.apache.commons.io import IOUtils
from org.apache.nifi.processor.io import StreamCallback
from string import Template


class PyStreamCallback(StreamCallback):
    def __init__(self):
        pass

    def process(self, inputStream, outputStream):
        try:
            input_text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
            incoming_flow = input_text.split('\n', 1)
            if len(incoming_flow) &amp;lt; 2:
                raise ValueError("Input data does not contain both template and values.")

            template_str, values_str = incoming_flow
            template_str = template_str.strip()
            values_str = values_str.strip()

            json.loads(template_str)
            parameter_obj = json.loads(values_str)

            tpl = Template(template_str)
            json_string = tpl.substitute(parameter_obj)

            # Replace placeholders and unwanted characters
            replacements = {
                "u'": "'",
                'u"': '"',
                '"{': '{',
                '}"': '}',
                "'": '"',
                "False": "false",
                "True": "true",
                '"[': "[",
                ']"': "]"
            }
            for old, new in replacements.items():
                json_string = json_string.replace(old, new)

            outputStream.write(bytearray(json_string.encode('utf-8')))

        except Exception as e:
            traceback.print_exc(file=sys.stdout)
            raise e


flowFile = session.get()
if flowFile is not None:
    try:
        flowFile = session.write(flowFile, PyStreamCallback())
        session.putAttribute(flowFile, 'mime.type', 'application/json')
        session.transfer(flowFile, REL_SUCCESS)
    except ValueError as ve:
        session.putAttribute(flowFile, 'error_val', str({"Script.Exception": str(ve)}))
        session.transfer(flowFile, REL_FAILURE)
    except Exception as e:
        session.putAttribute(flowFile, 'error_val', str({"Script.Exception": str(e)}))
        session.transfer(flowFile, REL_FAILURE)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Though there are a lot of string manipulation, so far it is working fine.&lt;/P&gt;&lt;P&gt;Since python is deprecated in the &lt;STRONG&gt;ExecuteScript&lt;/STRONG&gt; Processor. Was wondering if it would be possible to do this same operations using any other processor in NiFi without writing any of custom script.&lt;/P&gt;&lt;P&gt;we're using 1.24.0 version of NiFi.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can the ReplaceText processor be used to achieve this or is there any other recommendation?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2024 14:27:24 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Create-a-json-payload-using-a-template-and-its-values/m-p/390631#M247315</guid>
      <dc:creator>scoutjohn</dc:creator>
      <dc:date>2024-07-19T14:27:24Z</dc:date>
    </item>
    <item>
      <title>Re: Create a json payload using a template and its values</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Create-a-json-payload-using-a-template-and-its-values/m-p/390809#M247342</link>
      <description>&lt;P&gt;You could implement this using Groovy script which is forward and backward compatible. I've taken code written in NiFi 1.X and works just fine in 2.X&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2024 19:44:24 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Create-a-json-payload-using-a-template-and-its-values/m-p/390809#M247342</guid>
      <dc:creator>joseomjr</dc:creator>
      <dc:date>2024-07-22T19:44:24Z</dc:date>
    </item>
    <item>
      <title>Re: Create a json payload using a template and its values</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Create-a-json-payload-using-a-template-and-its-values/m-p/397112#M249697</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/105558"&gt;@joseomjr&lt;/a&gt;&amp;nbsp;, Thank you for responding,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I instead chose a hard way approach. I thought why not create a custom Nar, which takes in 2 parameters, 1 for the json template with placeholders and 2 with its respective values.&amp;nbsp;&lt;/P&gt;&lt;P&gt;used sort of a recursion to create the final output.&lt;/P&gt;&lt;P&gt;for and input like&amp;nbsp;&lt;/P&gt;&lt;P&gt;Template&lt;/P&gt;&lt;P&gt;&lt;EM&gt;{"details":{"name":"${name}","age":"${age}","superpower":"${superpower}"}}&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;value :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp;{"name": "Clark Kent", "age": "35","superpower": "Superman"}&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;gives output as&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;{&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;"details": {&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;"name": "Clark Kent",&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;"age": "35",&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;"superpower": "Superman"&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;}&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;}&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="scoutjohn_0-1730961774578.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/42509iC5E1E0CCF8A50007/image-size/medium?v=v2&amp;amp;px=400" role="button" title="scoutjohn_0-1730961774578.png" alt="scoutjohn_0-1730961774578.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="scoutjohn_1-1730961874052.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/42510i87387DED83140104/image-size/medium?v=v2&amp;amp;px=400" role="button" title="scoutjohn_1-1730961874052.png" alt="scoutjohn_1-1730961874052.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2024 06:47:45 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Create-a-json-payload-using-a-template-and-its-values/m-p/397112#M249697</guid>
      <dc:creator>scoutjohn</dc:creator>
      <dc:date>2024-11-07T06:47:45Z</dc:date>
    </item>
  </channel>
</rss>

