Run a JSONata expression against a sample feed entry on your machine before you add it to a data mapping in AI Hub.
jsonata library.The local setup approximates the AI Hub runtime. The bindings you create on this page are stand-ins for the default bindings that AI Hub adds to a data mapping, so results can differ from the real implementations. Confirm the final mapping by exporting your product data in AI Hub.
Prerequisites
- Node.js and npm installed on your machine.
- A terminal and a text editor.
Set up a test folder
jsonata library:mkdir jsonata-test
cd jsonata-test
npm init -y
npm install jsonata
Create a feed entry
feed-entry.json in the test folder.{
"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": { "type": "centPrecision", "centAmount": 12999, "currencyCode": "USD", "fractionDigits": 2 }
},
"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" }]
}
availability.channels object is keyed by inventory Channel ID. "inventory-channel-id" is a placeholder key in this sample. Your feed entries can also have no availability at all, for example when the variant has no inventory entry.To test with your own data, build a feed entry from a real product:
- Get the ProductProjection for the product in the Store that your Agentic Channel uses, and set it as
productData. - Choose one Product Variant and set it as
variant. To get the scopedpricefield on the variant, resolve the projection withpriceCurrencyand the price selection inputs that your Agentic Channel uses, such aspriceCountryandpriceChannel. A Store alone filters thepricesarray but doesn't setprice. - Set
storeto the Store andshippingMethodsto the Shipping Methods that apply.
productData, variant, store, and shippingMethods, or your expressions fail to find the values.Add stand-ins for bindings
$country, $locale, and $storeKey, and the functions $tail, $getAttribute, $storeBaseUrl, $priceToAmount, and $formatPrice. You can see them in the Bindings section of the Product sync tab. These don't exist outside AI Hub, so create stand-ins for the ones you use. Save the following as bindings.js.// Local stand-ins that mirror the default bindings in AI Hub.
const variables = {
country: 'US',
locale: 'en-US',
storeKey: 'my-store',
};
const functions = {
tail: (arr) => (Array.isArray(arr) ? arr.slice(1) : []),
getAttribute: (attributes, name) =>
(attributes || []).find((attribute) => attribute.name === name)?.value,
// Takes the price value object, for example variant.price.value
priceToAmount: (value) => {
if (!value) return '';
const digits = value.fractionDigits;
return value.type === 'centPrecision'
? value.centAmount / 10 ** digits
: value.preciseAmount / 10 ** digits;
},
// Takes the amount from $priceToAmount and the same price value object
formatPrice: (amount, value) =>
typeof amount === 'number' && value ? `${amount.toFixed(value.fractionDigits)} ${value.currencyCode}` : '',
};
module.exports = { variables, functions };
variables to match the bindings in your channel. The functions take the price value object, not the whole price. Chain them like the default mapping does, for example $priceToAmount(variant.price.value) ~> $formatPrice(variant.price.value).Run an expression
test.js. It loads the feed entry, registers the bindings, and evaluates the expression you pass as a command-line argument.const fs = require('fs');
const jsonata = require('jsonata');
const { variables, functions } = require('./bindings');
const expressionText = process.argv[2];
const entry = JSON.parse(fs.readFileSync('feed-entry.json', 'utf8'));
(async () => {
const expression = jsonata(expressionText);
for (const [name, fn] of Object.entries(functions)) {
expression.registerFunction(name, fn);
}
const result = await expression.evaluate(entry, variables);
console.log(JSON.stringify(result));
})();
Run an expression and wrap it in single quotes so your shell doesn't change it:
node test.js '$lookup(productData.name, $locale)'
The script prints the result:
"Classic Running Shoe"
node test.js '$lookup(productData.name, $locale) & " - " & $getAttribute(variant.attributes, "color") & " / " & $string($getAttribute(variant.attributes, "size"))'
"Classic Running Shoe - black / 42"
{ "en-US": "Black" }, or plain values. This expression returns the configured locale when the attribute is localized and otherwise returns the value as-is:node test.js '($value := $getAttribute(variant.attributes, "color"); $type($value) = "object" ? $lookup($value, $locale) : $value)'
Check for missing values
undefined. A tested real feed entry for SKU 12345 has an empty attributes array, so it has no material attribute. Load that entry as feed-entry.json to reproduce the lookup:node test.js '$getAttribute(variant.attributes, "material")'
undefined
$exists in a conditional expression:node test.js '$exists($getAttribute(variant.attributes, "material")) ? $getAttribute(variant.attributes, "material") : "not specified"'
"not specified"
Test with variants that have missing attributes or no price, because your catalog can contain products that the sample doesn't cover.
Check for errors
null price has no fractionDigits, so the manual conversion variant.price.value.centAmount / $power(10, variant.price.value.fractionDigits) throws an error. Guard the price first:node test.js 'variant.price ? $priceToAmount(variant.price.value) ~> $formatPrice(variant.price.value) : ""'
Argument 2 of function "power" does not match function signature. Select Open data mapping, fix the expression, and export again. Whether a required field that is empty causes problems depends on the provider. Check the requirements on the provider's product data page.Validate the mapping in AI Hub
The local script approximates AI Hub, so export your product data from AI Hub as a final check. The export evaluates every expression in the mapping against your real products. AI Hub has no preview for a single feed entry or product, so the exported CSV file is the way to see the mapped output.
The following steps save an empty value and an expression that fails every export. Saving applies your changes to a connected channel after a short redeploy, and AI Hub has no preview or dry run, so a failing expression goes live as saved. Switching off the sync toggles stops the feeds but doesn't make a test safe. Use a test channel that isn't connected to a provider, and restore your expression in the last step.
- In the Merchant Center, open your Agentic Channel and go to the Product sync tab.
- Edit the expression for one field and save the mapping. To see how empty values behave, use an expression that returns nothing for that field, such as
$getAttribute(variant.attributes, "does-not-exist"). - Export the product data for the channel and open the exported CSV file. The field you changed is empty for every variant, and the other columns keep their values.
- To see how errors behave, change the expression to one that can't be evaluated, such as
$power(10, null), and export again. The export fails and shows the function name and the SKU of the variant, as described in Check for errors. - Restore the expression that you want to keep, and export once more to confirm that the export succeeds and the values are correct.
Next steps
- Draft expressions faster with Generate a data mapping with AI.
- Look up bindings and the feed entry structure in JSONata data mapping.