Skip to main content
March 17, 2023
Question

Issue with a!toJson function

  • March 17, 2023
  • 8 replies
  • 0 views

Hi All,

Input code: a!toJson(
value: {
test: {
assuranceText: "Jag försäkrar att uppgifterna jag har lämnat är riktiga och bekräftar att jag har tagit del av informationen och villkoren ovan.\n\nDin ansökan grundas på automatiserad behandling av dina personuppgifter. På vår hemsida och under inställningar kan du läsa mera om hur vi behandlar dina personuppgifter och om dina rättigheter.\n\nNär du skickar in din ansökan kommer en kreditupplysning om dig att hämtas in ifrån UC"
}
},
removenulloremptyfields: true()
)

I am geeting the out put as below: in the output you can observer that one extra backword is added for the newline character

8 replies

richardm900458
March 17, 2023

Hi Manikumar,
can you mark the word you think is extra?

March 17, 2023

March 17, 2023

here I am expecting \n\n but in the output i am getting \\n\\n

mikes0011
Brainy
March 17, 2023

Your input doesn't have "newline characters" as far as a!toJson() is concerned, it just has the text "\n". So when you call a!toJson() on your original string, it's escaping your "\n" characters to "\\n", because it has no way of knowing you don't want it to preserve those as plaintext.

The Expression Guru
March 17, 2023

Hi Mike, the idea is in a json string we want to send "\n" in between the text so that other systems (built on different technology) will interpret it as new line and show the message with line breaks. so wanted to know how get the output from toJson("\n") as "\n" instead of "\\n"

mikes0011
Brainy
March 17, 2023

The first thing i'd try is to pre-sanitize it so that a!toJson() treats the newlines like newlines:

a!toJson(
  value: {
    test: {
      assuranceText: substitute(ri!input, "\n", char(10))
    }
  },
  removenulloremptyfields: true()
)

The Expression Guru