Forum Discussion

mohit3's avatar
mohit3
New Contributor
3 years ago

Generating wrong Json format for empty values

Hi All,

I am creating a Json file by using the Json formatter but I am not getting the json in correct format for all the empty values.

Below is the input for Json Formatter :

Getting below output from Json Formatter.

The output should look like below. Empty fields should be enclosed in double codes.
“Bankgroup” : “”,
“BankNumber” : “”,

Please let me know how we can achieve this.
@dmiller

Thanks
Mohit

5 Replies

  • Hi Mohit!
    What you might try is passing that JSON over to a mapper, and using the mapper to do what you want.
    Turn off pass through in the mapper and use an expression language statement to convert everything to a string like this:

    $.mapValues(x=>x == null ? "" : x.toString())
    

    This will essentially set everything to a string, even an empty string.
    Here’s what my mapper looks like to do this:

    Hope that gets you closer!
    – David

    • mohit3's avatar
      mohit3
      New Contributor

      @djsiegel I am not using the pass through. I am just mapping the fields in mapper snap and then sending it to Json formatter snap to generate the Json but it is only showing single quotation mark for empty values.

      I tried your expression as well but it is also not working.

  • Mohit, this is just an odd bug in the Designer’s viewer for JSON preview data. If you actually save the output of the JSON Formatter to a file using a File Writer, you’ll see that the empty string is a pair of double quotes as you expect.

    • djsiegel's avatar
      djsiegel
      Former Employee

      Can confirm, Patrick - I happened to just be building an example to test myself.
      When I download it, I do see the doublequotes.
      Mohit - here’s my example, with both the input and output. Hope that helps.

      CommunityExampleJSON.zip (4.3 KB)

      • ptaylor's avatar
        ptaylor
        Employee

        David, I downloaded and validated your pipeline. Even with the Mapper, the preview dialog for the JSON Formatter’s output shows the same problem with displaying a single double quote instead of two double quotes:

        Your Mapper doesn’t change these values since they are already strings.