The {json} function reads JSON data from a file (URL), decodes it and assigns it to a Smarty template variable.

It's also possible to assign only certain branches of nested arrays/objects to template variables which may be useful in cases where a single JSON file is used as some kind of configuration file for an entire project.

JSON objects can be converted to PHP objects or to associative arrays (default), using the obj2obj parameter.

Using the function:

{json file="filename.json" assign="data"}

assigns the decoded JSON data to $data, converting JSON objects to PHP associative arrays.

{json file="filename.json" assign="data" obj2obj=true}

does the same, but this time JSON objects are converted to PHP objects.

If the data passed to $data would have a property called 'languages', it is also be possible to assign only that property to a variable like this:

{json file="filename.json" lang="languages"}

$lang would now have the same value as $data.languages did in the first example (and $data->languages in the second).

And you can have more of these in one statement:

{json file="filename.json" lang="languages" coun="countries"}

Too bad this wiki does not accept the plugin code (problem with PRE element that is written in the debugging part of the code).

You can download the plugin from:

Discussion in Smarty forum: