Skip to content

JMESPath Cheat Sheet

Treestructure

Input

{
  "order": {
    "address": {
      "shipping": {
        "country": "NL"
      }
    }
  }
}

Wanted result

{
  "shippingCountry": "NL"
}

Jmespath

&{order.address.shipping.country}

Treestructure including strange characters

Some characters must be escaped. This includes: "-", ".", numbers (if they are at the beginning of a key), "#", "@"

Input

{
  "order": {
    "address": {
      "shipping": {
        "@country": "NL"
      }
    }
  }
}

Wanted result

{
  "shippingCountry": "NL"
}

Jmespath

&{order.address.shipping.\"@country\"}

Notes: In some cases in Alumio you only need to add double quotes ("") to the key. When using this inside of an object/JSON field, you also need to escape the quotation mark by adding \ before it. Because a dot (.) is important in Jmespath/JSON, you also need to escape it if a dot is part of the key name.

Arrays

Searching

Input

{
  "order": {
    "items": [
      {
        "sku": "123",
        "price": 150
      },
      {
        "sku": "456",
        "price": 250
      }
    ]
  }
}

Wanted Result

{
  "priceOfSku123": 150
}

Jmespath

&{order.items[?sku=='123'].price | [0]}

Notes: Because the 'sku' is a string (text), you need to use single quotations (''). If the search is a number, use backticks (``) instead. If you want to search for true or false, also use backticks like true, `false`.

Note that the | [0] is needed because the result is an array. This is true because you are doing a search on an array. This query can fail, because of the '| [0]'. If the result of the query is an empty array, the first item of the array will not exist and will fail. Add '|| `[]`' at the end to prevent this. This will return an empty array if the result is an empty array.

Jmespath allows these operators:

  • \== (equals)
  • != (not equals)
  • \< (less than)
  • \<= (less than or equal to)
  • > (greater than)
  • >= (greater than or equal to)
  • Further functions can be found at jmespath.org

You can also use two or more checks. For example: &{order.items[?sku=='123' && active==``true``].price | [0]} note, use single backticks with the true.

Getting last item of an array

Input

{
  "order": {
    "items": [
      {
        "sku": "123",
        "price": 150
      },
      {
        "sku": "456",
        "price": 250
      }
    ]
  }
}

Wanted Result

{
  "lastItem": {
    "sku": "456",
    "price": 250
  }
}

Jmespath

&{order.items[-1]}

Collapsing an array

Input

{
  "orders": [
    {
      "id": "ABC005",
      "items": [
        {
          "sku": "ABC",
          "price": 100
        }
      ]
    },
    {
      "id": "ABC006",
      "items": [
        {
          "sku": "DEF",
          "price": 100
        }
      ]
    }
  ]
}

Wanted Result

{
  "allOrderedItems": [
    {
      "sku": "ABC",
      "price": 100
    },
    {
      "sku": "DEF",
      "price": 100
    }
  ]
}

Jmespath

&{orders[].items[]}

Checks

Often you'll need to compare two fields. Alumio is capable of comparing a single field to a static value easily, but compare two dynamic fields is a bit more difficult. Jmespath is a good option for this, since it'll return a true/false value. That way, you can add a conditional transformer in Alumio which will check for the true/false.

Input

{
  "order": {
     "itemsOrdered": 5,
     "itemsShipped": 4
  }
}

Wanted Result

{
  "order": {
    "isFullyShipped": false
  }
}

Jmespath

&{order.itemsOrdered == order.itemsShipped}

Notes: You can use the following operators:

  • \== (equals)
  • != (not equals)
  • \< (less than)
  • \<= (less than or equal to)
  • > (greater than)
  • >= (greater than or equal to)
  • Further functions can be found at jmespath.org

You can also use two or more checks. For example: &{order.itemsOrdered == order.itemsShipped && order.itemsOrdered > 0}

Contains Check

Often, you'll want to know whether a certain value is in a larger dataset. For example, if you have a product SKU and want to know whether this SKU is in the list of products that are on sale.

Input

{
  "product": {
    "sku": "ABC"
  },
  "productsOnSale": [
    "ABC",
    "DEF",
    "GHI"
  ]
}

Wanted Result

{
  "product": {
    "isOnSale": true
  }
}

Jmespath

&{contains(productsOnSale,product.sku)}