The free Version of the JSON Content Importer-Plugin is based on the proprietary “JCI-parser” with some great features. This is good and still available.
But: If you want to have full control of your JSON, build applications etc. you need a better parser. For this the pro-plugin comes with the popular Twig-engine (https://twig.symfony.com/doc/1.x/templates.html).
The usage is as following:
After getting access to the JSON-data the PRO plugin can suggest you twig-code for displaying all the JSON-data.
For that create a template with the URL and maybe needed accessdata so that JSON is available. Leave the template empty and switch on the debugmode.
Preview the page and see the twig-code generated.
How to use Twig:
You can use Twig-code just the way as with jci-template-code in the free plugin (ok, but not recommended: use the PRO plugin-template, see below)
Example-Shortcode:
[jsoncontentimporterpro url=http://... parser=twig] {% for eventitem in event %} {% if eventitem.evs3|length > 0 %} {{eventitem.eventname}} {% endif %} {% endfor %} [/jsoncontentimporterpro]
The better way is to use Twig via the PRO plugin-templates:.
In short: Twig code is in the PRO plugin-template with Number NO. Then the Shortcode is as simple like this:
[jsoncontentimporterpro id=NO]
Twig-syntax:
- Twig is documented in detail here
- “Twig fiddle” is also great: Build and test your Twig-syntax together with your JSON
Sometimes the devil is in the detail:
- Handle dash in twig and JSON
- twig: Loop with key and value
- name of the loop: _context
- Nested Shortcodes
Twig has many powerful commands, but sometimes more is needed. Therefore in the PRO plugin several additional Twig-extensions are added:
- “sortbyjsonfield”: Sort JSON by chars, numbers or dates
- “sortbyarray” sorts an array accoring to the PHP-sorting-funkctions. Syntax: {{… | sortbyarray(SORTTYPE, SORTFLAG) }}. Valid values for SORTTYPE are the PHP-sortingfunctions “asort”, “arsort”, “rsort”, “ksort” and “krsort”. Default for SORTFLAG is SORT_REGULAR, see other valid flags here.
- format date: By “dateformat” you can set timezone and language / locale of time- and date-output. See example here.
- format number: “formatnumber(decimals, dec_point, thousands_sep)” formats numerical JSON-data
- “convert2html”: convert JSON-data into HTML
- “converthex2ascii”: convert hex-data from JSON to ascii
- “removespecialcharsinurl”: build URLs by lower strings, replacing blanks into dashes, converting “ä” into “ae” etc.
- “stringshorter(length, suffix)”: cut a string to length and put a suffix (e.g. “…”) at the end. This is useful for creating a Teasertext out of a long text
- “htmlspecialchars_decode”: like the PHP-function “htmlspecialchars-decode”
- “html_entity_decode”: like the PHP-function “html_entity_decode”: if the HTML-Tags in the JSON-data are encoded (e.g. < is <) use this to get real HTML. in case use with parameters like html_entity_decode(FLAG, ENCODING)
- “base64encode”: like the PHP-function “base64_encode”
- “htmlentities”: like the PHP-function “htmlentities”
- “removenonprintable”: strip everything except basic printable ASCII characters by preg_replace( ‘/[^[:print:]]/’, replacement, input)
- “preg_replace(PATTERN, REPLACEMENT)”: like the PHP-function “preg_replace”
- “md5”: like the PHP-function “md5”
- “dump”: output JSONARRAY by PHP-syntax print_R(JSONARRAY, TRUE)
- Usage of twig for JSON starting with “[{“: See twig and it’s _context
Especially if you want to merge several JSON-Feeds to one JSON-Feed “doshortcode” and “json_decode” are helpful: By “doshortcode” you can execute other JSONContentImporter-Shortcodes whcih give JSON and “json_decode” tranforms the Shortcode-Strings to twig-arrays which can be merged into one twig-array:
- “json_decode”: If a twig-variable contains a JSON-string you can create an twig-array: {{% set myArray = (jsonString |json_decode) %}this json_decode works like the PHP json_decode
- “doshortcode”: Execute the WordPress do_shortcode in a string: {% set jsonArr = (string | doshortcode)%}