Portals webtemplates – create json – json not parseable

In nearly every portalproject, i have to create a page which reads some data from crm dynamics and formats it as json to be consumed in a clientscript. This is well documented (ok, this is XML, but you will get the story) and usually no problem. But sometimes the json is invalid and the jsonparser fails. The number one reason in my projects is, that the text in the crm field contains tabs or newlines which is not allowed in json. So, we have to “pipe” it out in the infected fields :-)

In order to do so, use:

...
,"content":"{{ item.content | replace: '"' , '\"' | newline_to_br | strip_newlines | replace: " ", ''}}"
..


Attention: the replace contains a tabcharacter and not a blank. Sometimes, you have to replace “\t”, too…

Of Course you can choose between newline_to_br and strip_newlines but to keep it in mind for me, i used both :-)

, ,

No comments yet.

Leave a Reply