Hey Hollenberg.
I created this Freemarker Macro which does exactly what you want. 😊
convertToJson Input any object and print it to a JSON string.
<#macro convertToJson myHash level=0 escape=false>
<#local _spacer = " ">
<#if (myHash?is_hash)!false>{<#if (myHash?is_hash_ex)!false><#list myHash as key, value>
${""?left_pad((level+1), _spacer)}"${key}": <#if (value?is_string)!false><#if escape!false>"${value?json_string}"<#else>"${value}"</#if><#elseif (value?is_number)!false>${value}<#elseif (value?is_boolean)!false>${value?boolean}<#elseif (value?is_hash)!false><@convertToJson myHash=value level=(level+1) escape=escape/><#elseif (value?is_sequence)!false><#if (value?has_content)!false><@convertToJson myHash=value level=(level+1) escape=escape/><#elseif (value?size == 0)!false>[]<#else>"macroError: Sequence value/content could not be determined or tested."</#if><#elseif (value?is_method)!false>"THIS is a METHOD"<#elseif (value?is_directive)!false>"THIS is a Directive"<#elseif (!value?has_content)!false>${value?cn}<#else>"macroError: Value type could not be determined."</#if><#sep>,</#list>
${""?left_pad(level, _spacer)}</#if>}<#elseif (myHash?is_sequence)!false>[<#list myHash as _array><#if (_array?is_hash)!false>
${""?left_pad((level+1), _spacer)}<@convertToJson myHash=_array level=(level+1) escape=escape/><#if _array?is_last>
${""?left_pad(level, _spacer)}</#if><#elseif (_array?is_string)!false><#if escape!false>"${_array?json_string}"<#else>"${_array}"</#if><#elseif (_array?is_number)!false>${_array}<#elseif (_array?is_boolean)!false>${_array?boolean}<#elseif (!_array?has_content)!false>${_array?cn}</#if><#sep>,</#list>]</#if><#return></#macro>
In your case I would assign a variable, lets name it “myJsonText”, and give it the following value.
<#macro convertToJson myHash level=0 escape=false>
<#local _spacer = " ">
<#if (myHash?is_hash)!false>{<#if (myHash?is_hash_ex)!false><#list myHash as key, value>
${""?left_pad((level+1), _spacer)}"${key}": <#if (value?is_string)!false><#if escape!false>"${value?json_string}"<#else>"${value}"</#if><#elseif (value?is_number)!false>${value}<#elseif (value?is_boolean)!false>${value?boolean}<#elseif (value?is_hash)!false><@convertToJson myHash=value level=(level+1) escape=escape/><#elseif (value?is_sequence)!false><#if (value?has_content)!false><@convertToJson myHash=value level=(level+1) escape=escape/><#elseif (value?size == 0)!false>[]<#else>"macroError: Sequence value/content could not be determined or tested."</#if><#elseif (value?is_method)!false>"THIS is a METHOD"<#elseif (value?is_directive)!false>"THIS is a Directive"<#elseif (!value?has_content)!false>${value?cn}<#else>"macroError: Value type could not be determined."</#if><#sep>,</#list>
${""?left_pad(level, _spacer)}</#if>}<#elseif (myHash?is_sequence)!false>[<#list myHash as _array><#if (_array?is_hash)!false>
${""?left_pad((level+1), _spacer)}<@convertToJson myHash=_array level=(level+1) escape=escape/><#if _array?is_last>
${""?left_pad(level, _spacer)}</#if><#elseif (_array?is_string)!false><#if escape!false>"${_array?json_string}"<#else>"${_array}"</#if><#elseif (_array?is_number)!false>${_array}<#elseif (_array?is_boolean)!false>${_array?boolean}<#elseif (!_array?has_content)!false>${_array?cn}</#if><#sep>,</#list>]</#if><#return></#macro>
{
"source”: "WH-AR-00”,
"body”: <@convertToJson myHash=_request["body"] level=1 escape=true>
}
From there on, you can simply add ${_variables[“myJsonText”]} to insert the whole the json body as a string.
OR
Use ?eval_json Freemarker built-in to have Freemarker treat the string as a JSON object.
Which enables you to do the following.
${(_variables[“myJsonText”]?eval_json)[“source”]}
I have used this macro in a lot of projects, since it allows me to print any type of object, to a JSON valid string.
The macros “myHash” input support everything, not just hashes. 😁
So anything Freemarker treats as an object, can be converted into a JSON valid string.
Have Fun 👋😋
Best Regards.
Jeppe Gents
(Former ‘Solutions & Integration Specialist’ employee at TOPdesk Denmark A/S)