Freemarker parse a String as Json

freemarker

Solution

Use `?eval_json` (requires FreeMarker 2.3.31):

<#-- Using '...' instead of "..." for convenience: no need for \" escapes this way. -->
<#assign test = '{"foo":"bar", "f":4, "text":"bla bla"}'>
<#assign m = test?eval_json>

${m.foo}  <#-- prints: bar -->

<#-- Dump the whole map: -->
<#list m as k, v>
  ${k} => ${v}
</#list>

Before 2.3.31, `?eval` was popular for this purpose, but that actually expects FreeMarker expressions. That's a problem because it doesn't support `null`, or `\uXXXX` escapes (so parsing of such JSON will fail). Also it can be a security problem, because it supports accessing variables, and calling methods/functions, while JSON doesn't.

Problem

Probably it's not possible, but I would like to transform a json string in a map with freemarker ex: ``` <#assign test = "{\"foo\":\"bar\", \"f\":4, \"text\":\"bla bla\"}"> ``` and be able to get the text key from this string

Original source