Skip to content

Documentation

JavaScript SDK: Collecting JSON and reading scenario parameters

Some websites already expose the needed data as JSON. This is usually simpler than parsing HTML text. Use the source intended for your scenario: ctx.http() applies its network settings and limits.

JSON response

await ctx.http().get(url) returns a Response; response.json() parses its existing body locally. Invalid JSON throws. Verify the response shape before reading fields.

async function run(ctx) {
  const url = ctx.param('api_url', '');
  if (!url) ctx.abort('Enter a data URL');
  const response = await ctx.http().withHeaders({ Accept: 'application/json' }).get(url);
  if (!response.ok()) ctx.fail(`Source returned HTTP ${response.status}`);
  const payload = response.json();
  if (!payload || !Array.isArray(payload.items)) ctx.fail('Product list is missing');

  for (const item of payload.items) {
    const sku = String(item.sku ?? '');
    if (!sku) continue;
    const price = ctx.text().money(item.price);
    ctx.emit({ sku, name: String(item.name ?? ''), price }, sku);
  }
}

Use ctx.json(text) when JSON is already in a string field of a document or browser response. This method has no depth argument. Check whether an optional key exists rather than assuming it always does.

Parameters and secrets

ctx.param(name, fallback) reads one parameter; ctx.params() returns the complete parameter object. Use input names from scenario settings. Read a named secret separately with await ctx.credential(name) and never put it in records or logs. If there are several URLs, pass an array and validate its type before iterating.

JSON errors and retry

Check the HTTP status before calling response.json() because error pages often contain HTML. If the source schema changes, use ctx.fail() with a useful reason. You may catch a JSON parse error, but do not silently replace it with an empty list: that would look like the end of the catalog.

Response · TextTools · Pagination