.. include:: ../../Includes.txt .. _JsonHelper: ============================================== JsonHelper ============================================== \\nn\\t3::JsonHelper() ---------------------------------------------- The script helps convert and parse JavaScript object strings into an array. .. code-block:: php $data = \Nng\Nnhelpers\JsonHelper::decode( "{title:'Test', cat:[2,3,4]}" ); print_r($data); The helper makes it possible to use the JavaScript object notation in TypoScript and convert it to an array via the ``{nnt3:parse.json()}`` ViewHelper. This is handy if, for example, slider configurations or other JavaScript objects should be defined in TypoScript to be used later in JavaScript. Another usage example: you want to use the "normal" JS syntax in a ``.json`` file, instead of the JSON syntax. Let's look at an example. This text was written to a text file and is to be parsed via PHP: .. code-block:: php // Contents of a text file. { Example: ['one', 'two', 'three'] } PHP would report an error with ``json_decode()`` for this example: The string contains comments, wraps, and the keys and values are not enclosed in double quotes. However, the JsonHelper or the ViewHelper ``$jsonHelper->decode()`` can convert it easily. This is how you could define a JS object in the TypoScript setup: .. code-block:: php // Contents in TS setup. my_conf.data ( { dots: true, sizes: [1, 2, 3] } ) The mix is a little irritating: ``my_conf.data (...)`` öffnets a section in the TypoScript for multi-line code. Between the ``(...)`` is then a "normal" JavaScript object. This can then be easily used as an array in the Fluid template: .. code-block:: php {nnt3:ts.setup(path:'my_conf.data')->f:variable(name:'myConfig')} {myConfig->nnt3:parse.json()->f:debug()} Or append as a data attribute to an element to parse it later via JavaScript: .. code-block:: php {nnt3:ts.setup(path:'my_conf.data')->f:variable(name:'myConfig')}