{% extends "admin/change_form.html" %} {% block extrastyle %} {{ block.super }} {% endblock %} {% block content %} {{ block.super }}

Query examples

For example consider the source:
{"person": {"name": "Bob", "age": 42}}
the query .person.name displays Bob
the query .person.age displays 42

Another example of source:
[1, 2, 3]
the query .[] or .[0] displays 1
the query .[1] or .[]+1 displays 2
the query .[2] or .[]+2 displays 3
the query . displays [1, 2, 3]
the query .[0:2] displays [1, 2]

Complex example:
[
    {"currency": "USD", "amount": 2230},
    {"currency": "EUR", "amount": 22500},
    {"currency": "GBP", "amount": 222000}
]
the query .[] | select(.currency == "EUR").amount displays 22500
the query .[] | select(.amount < 2240).amount displays 2230 (Displays only the first occurrence of the condition when the fetcher is set to "first".)
the query .[] | select(.amount < 2240) | "\(.amount) \(.currency)" displays 2230 USD
the query max_by(.amount).currency displays GBP
the query min_by(.amount).currency displays USD
the query .[] | "<tr><td>\(.currency)</td><td>\(.amount)</td></tr>" with function "all" and wrapper "<table>{}</table>" and checked checkbox "Mark safe" displays the table
USD2230
EUR22500
GBP222000
the query def format: tostring | [while(length > 0; .[:-3]) | .[-3:]] | reverse | join(" ") + " CZK"; .[] | "<tr><td>\(.currency)</td><td>\(.amount|format)</td></tr>" with function "all" and wrapper "<table>{}</table>" and checked checkbox "Mark safe" displays the table
USD2 230 CZK
EUR22 500 CZK
GBP222 000 CZK

For more examples see jq on pypi.org.
The function of querying data is jq.compile(query).input_value(source).$fetcher().
More resources for studying query:
  1. jq Manual
  2. JQ Examples
  3. About Strings
  4. jq play - a playground

{% endblock %}