twig: Sorting JSON

The PRO Version can sort JSON by using a JCI-twig-extension.
Example-JSON: https://api.json-content-importer.com/extra/json/travel.json

This is done like this:

{% for item in JSONArray | sortbyjsonfield("fieldA##fieldB,desc##fieldC,,num") %}
fieldA: {{item.fieldA}}  / fieldB: {{item.fieldB}} / fieldC: {{item.fieldC}}
{% endfor %}

Syntax:

sortbyjsonfield(STRING) with a special string like “fieldname” (sort field as string from A to Z)

  • “fieldname,desc” (sort field as string from Z to A)
  • “fieldname,,num” (sort field as number from -infinity to infinity)
  • “fieldname,desc,num” (sort field as number from infinity to -infinity)
  • “fieldname,,date” and “fieldname,desc,date” (sort field as date)

“,” and “##” are delimiters (if those are part of the fieldname this won’t work – contact developer in this case to make the delimiter dynamic).

Here we use it like this:


{% for i in Offers | sortbyjsonfield("Refnum,desc,") | slice(0,5) %}
    {{i.Refnum}}: {{i.Arraptname}}<br>
{% endfor %}

In action:

WEBPCK1919-276: Tenerife, Sur Int.(Reina Sofia
WEBPCK1919-275: Tenerife, Sur Int.(Reina Sofia
WEBPCK1919-274: Tenerife, Sur Int.(Reina Sofia
WEBPCK1919-273: Lanzarote (Arrecife) Int.
WEBPCK1919-272: Lanzarote (Arrecife) Int.