Use JSON data in email chunk


In a Formit form I have an input to transmit JSON data to a snippet inside email template.

In the template I get the data but when the JSON data is passed as snippet option, it doesn’t work

* jsonstring exemple: {"cat":"logistique","depense":"4","reducper":"20","eco":"0.8"}
* [[!processJSon? &jsonstring=`[[+jsonstring]]`]]

/*  case 1 nan */
$someJSON =  $modx->getOption('jsonstring', $scriptProperties);

/* case 2 nan, but data has the same formatting than case 3...  */
$someJSON_bkt = "[$someJSON]";
/* [{"cat":"logistique","depense":"4","reducper":"20","eco":"0.8"},"cat":"rh","depense":"5","reducper":"8","eco":"0.4"}] */
$output .= "someJSON_bkt getoption: " . $someJSON_bkt . "<br>";

/* case 3 works if JSON data is specified directly inside the snippet and not from snippet option */
$someJSON_dur = '[{"cat":"locaux","depense":"444","reducper":"12.2","eco":"54.17"}]';
$output .= "someJSON_dur: " . $someJSON_dur . "<br>";

 $someObject = json_decode($someJSON,false);
 $output .= "someObject: " . $someObject ."<br>";
 $output .= 'single object output:' . $someObject[0]->cat;

  return $output;

did you try to call it with cached tags?

You probably have to use the allowSpecialChars validator for the jsonstring field in your form.


Otherwise the character " in the JSON data gets replaced with &quot;