<?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: how to replace empty string with null in nested json? in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/how-to-replace-empty-string-with-null-in-nested-json/m-p/381422#M244308</link>
    <description>&lt;P&gt;ExecuteGroovyScript alternative with this input&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;{
  "idTransakcji": "123",
  "date": "",
  "name": "sam"
}&lt;/LI-CODE&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; 
                                        Map data = jsonSlurper.parse(inputStream)
                                        data = [
                                            "id": data.idTransakcji,
                                            "user": [
                                                "date": data.date?.isNumber() ? Long.parseLong(data.date) : null,
                                                "name": data.name
                                            ]
                                        ]
                                        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;</description>
    <pubDate>Thu, 28 Dec 2023 21:02:13 GMT</pubDate>
    <dc:creator>joseomjr</dc:creator>
    <dc:date>2023-12-28T21:02:13Z</dc:date>
    <item>
      <title>how to replace empty string with null in nested json?</title>
      <link>https://community.cloudera.com/t5/Support-Questions/how-to-replace-empty-string-with-null-in-nested-json/m-p/381399#M244297</link>
      <description>&lt;P&gt;Hi guys! I have json like that:&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"id" : "20",&lt;BR /&gt;"user" : {&lt;BR /&gt;"name" : "John",&lt;BR /&gt;"date" : ""&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;I transform this date to Long using nifi langugage&amp;nbsp;${date:isEmpty():ifElse('',${date:toDate('yyyy-MM-dd HH:mm:ss'):toNumber()})} and after this i do in jolttrasformation something like&lt;/P&gt;&lt;P&gt;[&lt;BR /&gt;{&lt;BR /&gt;"operation": "shift",&lt;BR /&gt;"spec": {&lt;BR /&gt;"idTransakcji" : "id",&lt;BR /&gt;"date": "user.date",&lt;BR /&gt;}&lt;BR /&gt;},&lt;BR /&gt;{&lt;BR /&gt;"operation": "modify-overwrite-beta",&lt;BR /&gt;"spec": {&lt;BR /&gt;"user": {&lt;BR /&gt;"date": "=toLong(@(1,date))"&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;]&lt;/P&gt;&lt;P&gt;&amp;nbsp;and it works. But I have problem beacause when I have emptyString in this date i need to transfer it in null(not null in String) and i dont know how to do it using JoltTransformation or something else&amp;nbsp; so as not to lose what I already have and have null only when there is an empty string.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Dec 2023 14:13:13 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/how-to-replace-empty-string-with-null-in-nested-json/m-p/381399#M244297</guid>
      <dc:creator>MWM</dc:creator>
      <dc:date>2023-12-28T14:13:13Z</dc:date>
    </item>
    <item>
      <title>Re: how to replace empty string with null in nested json?</title>
      <link>https://community.cloudera.com/t5/Support-Questions/how-to-replace-empty-string-with-null-in-nested-json/m-p/381415#M244305</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/105912"&gt;@MWM&lt;/a&gt; ,&lt;/P&gt;&lt;P&gt;replacing empty string with null is kind of tricky thing with jolt. Unfortunately there is no direct way that I know of but hopefully my comments will make it easy for you to understand.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Based on your spec I assume you have the following input:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{
  "idTransakcji": "123",
  "date": "",
  "name": "sam"
}&lt;/LI-CODE&gt;&lt;P&gt;The jolt spec would be like this ( comments start with //):&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[
  {
    "operation": "shift",
    "spec": {
      "idTransakcji": "id",
      "date": {
        // Parse date value 
        // if its empty string ""
        "": {
          //remove the date from the output    	
          "": "user.date"
        },
        //For any values other than ""
        "*": {
          //assign the actual value ($) to user.date
          "$": "user.date"
        }
      },
      // Assign other fields like name under user
      "*": "user.&amp;amp;"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "user": {
        // if the date is found it means that we
        // should have a value, therefore convert to long
        "date": "=toLong(@(1,date))"
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "user": {
        // if the date is not found then set to null
        // default transformation only works when 
        // the key doesnt exist.
        "date": null
      }
    }
  }
]&lt;/LI-CODE&gt;&lt;P&gt;You can try the spec here : &lt;A href="https://jolt-demo.appspot.com/#inception" target="_blank"&gt;https://jolt-demo.appspot.com/#inception&lt;/A&gt;&lt;/P&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;</description>
      <pubDate>Thu, 28 Dec 2023 20:16:37 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/how-to-replace-empty-string-with-null-in-nested-json/m-p/381415#M244305</guid>
      <dc:creator>SAMSAL</dc:creator>
      <dc:date>2023-12-28T20:16:37Z</dc:date>
    </item>
    <item>
      <title>Re: how to replace empty string with null in nested json?</title>
      <link>https://community.cloudera.com/t5/Support-Questions/how-to-replace-empty-string-with-null-in-nested-json/m-p/381422#M244308</link>
      <description>&lt;P&gt;ExecuteGroovyScript alternative with this input&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;{
  "idTransakcji": "123",
  "date": "",
  "name": "sam"
}&lt;/LI-CODE&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; 
                                        Map data = jsonSlurper.parse(inputStream)
                                        data = [
                                            "id": data.idTransakcji,
                                            "user": [
                                                "date": data.date?.isNumber() ? Long.parseLong(data.date) : null,
                                                "name": data.name
                                            ]
                                        ]
                                        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;</description>
      <pubDate>Thu, 28 Dec 2023 21:02:13 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/how-to-replace-empty-string-with-null-in-nested-json/m-p/381422#M244308</guid>
      <dc:creator>joseomjr</dc:creator>
      <dc:date>2023-12-28T21:02:13Z</dc:date>
    </item>
    <item>
      <title>Re: how to replace empty string with null in nested json?</title>
      <link>https://community.cloudera.com/t5/Support-Questions/how-to-replace-empty-string-with-null-in-nested-json/m-p/381450#M244319</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/80381"&gt;@SAMSAL&lt;/a&gt;&amp;nbsp;your solution is workink, thank you so much!&lt;/P&gt;</description>
      <pubDate>Fri, 29 Dec 2023 13:25:07 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/how-to-replace-empty-string-with-null-in-nested-json/m-p/381450#M244319</guid>
      <dc:creator>MWM</dc:creator>
      <dc:date>2023-12-29T13:25:07Z</dc:date>
    </item>
  </channel>
</rss>

