jq queries

Reshape data with jq, including the built-in functions

  1. Run a JSON Query (jq) on an Object
  2. Built-in JQ Functions

Run a JSON Query (jq) on an Object

Reshape an object, for example the value of another field, with a jq expression:

fn.fnJq($(settings),'.mapping | keys')

// output : a full json object taken from another field called "settings", converted by jq

// fn.fnJq(object, jq)
// - object, for example read from rest or yaml file
// - jq-expression : a jq-expression (https://jqplay.org)
// - wrap the result in fn.fnSort to order it

From 7.3.0 a jq filter runs at most JQ_TIMEOUT_SECONDS (30 by default) before it is stopped, and its output is capped at 64 MB. This applies to fn.fnJq and to the jq expression of the REST, file, credential and database query functions.


Built-in JQ Functions

All data-fetching functions accept a JSON query (jq), a language to manipulate data objects and arrays.
When manipulating data, you may need to convert bytes to KB, MB or GB, or to round numbers.
For this purpose, AnsibleForms includes a few custom jq functions:

  • fn2KB: Bytes to KB
  • fn2MB: Bytes to MB
  • fn2GB: Bytes to GB
  • fnRound0: Round with 0 decimals
  • fnRound1: Round with 1 decimal
  • fnRound2: Round with 2 decimals
  • fnRound: Round with 2 decimals
fn.fnRestBasic('get','https://youruri','','CREDS',
  '[.records[] | {"Available Capacity":.storage_capacity.available | fn2GB | fnRound }]')
// note you need to pipe into the functions