JSONata data mapping

Ask about this Page
Copy for AI
View as Markdown

Use JSONata expressions in data mappings inside the Product sync module to transform your commercetools product data into the format required by the AI provider.

JSONata is a lightweight query and transformation language for JSON data. In AI Hub, JSONata expressions power the Data mapping configuration in the Product sync tab, where you define how product fields map to the target feed schema.

Each expression is evaluated against a single entry in the product feed. Expressions can access product fields directly, apply conditional logic, and call built-in helper functions called bindings.

For instructions on accessing the data mapping editor, see Data mapping.

Product data object

The product feed is flattened by variant: each entry in the feed represents one product variant, not a product. A product with two variants produces two separate feed entries. Your expressions always run in the context of a single variant entry.
The payload structure follows standard commercetools ProductProjection resource data. Each feed entry has the following top-level fields:
  • productData: the ProductProjection for the current product. In productData, the masterVariant and variants fields contain only the id, key, and sku of each Product Variant.
  • variant: the Product Variant that this feed entry represents, with all of its fields.
  • store: the Store of the Agentic Channel.
  • shippingMethods: the ShippingMethod resources that match the country of the Agentic Channel, with the zone of each Zone rate expanded.
The ProductProjection is resolved in the context of the Store, using the country, currency, and Product distribution channel of the Agentic Channel for price selection. As a result, the variant includes a scoped price field (singular). For mapping expressions, prefer variant.price over the variant.prices array as the price source.
The following references are expanded by default: categories[*].ancestors[*], and the discount of the scoped price of each Product Variant. To expand additional references, add them as Product Expansions in the data mapping configuration.

The following simplified example shows the structure of a single feed entry. Fields not relevant to data mapping are omitted.

Feed entryjson
{
  "productData": {
    "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "name": { "en-US": "Classic Running Shoe", "de-DE": "Klassischer Laufschuh" },
    "description": { "en-US": "A versatile everyday running shoe." },
    "slug": { "en-US": "classic-running-shoe" },
    "masterVariant": {
      "id": 1,
      "key": "classic-running-shoe-master",
      "sku": "CRS-001-BLK-42"
    },
    "variants": [
      {
        "id": 2,
        "key": "classic-running-shoe-v2",
        "sku": "CRS-001-BLK-43"
      }
    ]
  },
  "variant": {
    "id": 1,
    "key": "classic-running-shoe-master",
    "sku": "CRS-001-BLK-42",
    "price": {
      "id": "a1b2c3d4-5678-4abc-b3fc-2c963f66afa6",
      "value": { "centAmount": 12999, "currencyCode": "USD", "fractionDigits": 2 },
      "channel": { "id": "956d220a-fde7-44be-8496-c852fed723ca" }
    },
    "attributes": [
      { "name": "color", "value": "black" },
      { "name": "size", "value": 42 }
    ],
    "availability": {
      "channels": {
        "inventory-channel-id": {
          "isOnStock": true,
          "availableQuantity": 100
        }
      }
    }
  },
  "store": {
    "id": "store-ABC"
  },
  "shippingMethods": [
    {
      "id": "sm-standard"
    },
    {
      "id": "sm-express"
    }
  ]
}

This example shows how multiple resources are combined in a single feed entry. For full field-level details, refer to the API type definitions:

Default bindings

Bindings are values and helper functions that AI Hub makes available to every expression in a data mapping. You can see them in the Bindings section of a channel's Product sync tab, where you can also edit them or add your own. Provider templates can add or change bindings, so check the Bindings section of your channel.

Variable bindings

The following variable bindings are available in every expression. Their values are derived from the configuration of your Agentic Channel, and cannot be reassigned. They are always injected and read-only.

BindingTypeExample valueDescription
$countrystring"US"Country configured for the Agentic Channel.
$localestring"en-US"Locale configured for the Agentic Channel.
$storeKeystring"us-store"Key of the Store of the Agentic Channel.

Function bindings

The default templates of all providers include the following function bindings. You can modify or remove them, and add your own.

BindingDescription
$tail(array)Returns the array without its first item. For example, $join($tail(variant.images).url, ",") lists all images except the first.
$getAttribute(attributes, name)Returns the value of a named attribute from a variant's attributes array.
$priceToAmount(money)Returns the numeric amount of a TypedMoney value, such as variant.price.value, accounting for fractionDigits. Supports both cent precision and high precision money values. Returns an empty string if there is no price.
$formatPrice(amount, money)Returns the amount formatted with the number of fraction digits of the money value, followed by its currency code (for example, "129.99 USD").
$storeBaseUrl(store)Returns the base URL of the storefront. Uses the sellerStoreUrl Custom Field of the Store if set. Otherwise, returns the origin of the Store's storefront.orderUrlTemplate or storefront.checkoutUrlTemplate.
$tail(array)Returns all elements of an array except the first one, for example, to list additional images.

Expression examples

The following examples show expressions of increasing complexity, from direct field access to derived transformations.

Access fields directly

The simplest expressions access a field directly from the product entry.

Get the variant SKU (feed product ID):
Get variant SKUtext
variant.sku
Get the product name in a specific locale:

Localized fields are plain objects keyed by locale. Use a string key with backticks when the locale contains a hyphen.

Get localized product nametext
productData.name.`en-US`
Get the product ID:
Get product IDtext
productData.id

Use bindings and conditionals

Intermediate expressions use bindings, conditionals, or string operations.

Get the product name in the configured locale:
Use the $locale variable binding and $lookup to access the localized value dynamically, without hardcoding a language.
Dynamic locale lookuptext
$lookup(productData.name, $locale)
Map stock availability to a feed-compatible string:
Availability is nested under variant.availability.channels and keyed by inventory Channel ID. Stock that has no supply Channel is in the top-level variant.availability.isOnStock field. A variant without inventory has no availability. The Google default template maps a variant to "in_stock" if any Channel has stock, and to "out_of_stock" otherwise. The OpenAI default template does the same, but uses the top-level isOnStock value when the variant has no Channels.
Map stock availabilitytext
variant.availability.channels ? ($count(variant.availability.channels.*[isOnStock=true]) > 0 ? "in_stock" : "out_of_stock") : "out_of_stock"

This expression counts stock in every inventory Channel. To use the top-level value when the variant has no Channels, add a fallback:

Map stock availability with a top-level fallbacktext
variant.availability.channels ? ($count(variant.availability.channels.*[isOnStock=true]) > 0 ? "in_stock" : "out_of_stock") : (variant.availability.isOnStock ? "in_stock" : "out_of_stock")
Get a custom attribute value by name:
Use $getAttribute to retrieve a named attribute from the variant's attributes array.
Get attribute by nametext
$getAttribute(variant.attributes, "color")

Build derived values

Complex expressions combine multiple fields, optional helper functions, and logic to produce derived values.

Extract a numeric price amount when a price is present:
Use $priceToAmount with the money value of the scoped price to correctly account for the currency's fractionDigits value.
Check that the variant has a price before you convert it. $priceToAmount takes the price value object and accounts for the currency's fractionDigits value.
Extract numeric pricetext
variant.price ? $priceToAmount(variant.price.value) : ""
If you convert the amount yourself, guard the price in the same way. Without the guard, a variant without a price makes $power fail and stops the export.
Manual fractionDigits conversiontext
variant.price ? variant.price.value.centAmount / $power(10, variant.price.value.fractionDigits) : ""
Return a formatted price string:
Pass the amount and the money value to $formatPrice. The default templates use the JSONata chain operator ~> to pass the result of $priceToAmount as the first argument. The result includes the currency code, for example "79.99 EUR".
Format price stringtext
variant.price ? $priceToAmount(variant.price.value) ~> $formatPrice(variant.price.value) : ""
Return the sale price:
The discounted price is under variant.price.discounted.
Format sale pricetext
variant.price.discounted ? $priceToAmount(variant.price.discounted.value) ~> $formatPrice(variant.price.discounted.value) : ""
Build a title from the product name and variant attributes:

Combine the localized product name with custom attribute values to produce a richer title string.

Build composite titletext
$lookup(productData.name, $locale) & " - " & $getAttribute(variant.attributes, "color") & " / " & $string($getAttribute(variant.attributes, "size"))
When an expression returns no value or an empty string, the field is empty in the exported product data, and the product is still exported. When an expression can't be evaluated, the export fails. AI Hub shows an Export failed message with the function name and the SKU of the variant, for example Argument 2 of function "power" does not match function signature. Select Open data mapping, fix the expression, and export again.

Each provider validates empty fields and wrong formats on its own side, and AI Hub doesn't show the results of that validation. A provider can skip a product that fails its checks:

  • Google skips a product that has an empty ID.
  • OpenAI skips a variant that has no price in the configured currency.

Check the required fields on the provider's product data page before you sync. After a sync, check the provider's dashboard for validation errors, and fix them in your data mapping.

Handle missing data with valid JSONata conditional expressions, such as $exists(value) ? value : fallback. JSONata does not provide a $default operator.

Next steps