×

Please give details of the problem

Skip to content

How to create a JSONObject

Code

<#assign firstname = "John">
<#assign lastname = "Smith">
<#assign my_json>
    { "firstname": "${firstname}", "lastname": "${lastname}" }
</#assign>
<#assign my_json = my_json?eval>
${my_json}

NB: This code solve "unquotted string" issue (cf below)

Don't do this :

<#assign firstname = "John">
<#assign lastname = "Smith">
<#assign my_json = { "firstname": firstname, "lastname": lastname }>
${my_json}

Result : Error

Error while processing <#assign firstname = "John">
<#assign firstname = "John">
<#assign lastname = "Smith">
<#assign my_json = { "firstname": firstname, "lastname": lastname }>
${my_json}
Unquotted string 'firstname'

Instead do :

<#assign firstname = "John">
<#assign lastname = "Smith">
<#assign my_json>
    { "firstname": "${firstname}", "lastname": "${lastname}" }
</#assign>
${my_json}

Result :

{ "firstname": "John", "lastname": "Smith" }