Http request 'optimize response' with multiple items

Hello,

I have an “http request” tool for an AI agent. The API returns a JSON with multiple items in the form of an “array” (I’m not a JSON expert).

I enabled the “Optimize Response” option and I would like to reduce the exploitation by the LLM to a few fields. In the “Field” field I put:

It works but of course this limits the data to item 0. How can I make it loop over all the values of “items”?

Thank you in advance,
Philippe.

Information on your n8n setup

  • n8n version: 2.39.7
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Linux

Hey @flipflip, while you wait for a response, here are some things that might help:

Suggested resources

Automatically matched to your question.

Docs:

Forum:

@Joy_Ful, @jabbson, @Franz - you’ve helped with similar issues before, can you take a look?

Automatically suggested by n8n’s community bot. It’s a pilot - please share feedback here.

Hi @flipflip, items[0] selects only the first product. In 2.39.7, this option doesn’t support items[*], and _embedded.items won’t work in “Field Containing Data” either.

Use a Call n8n Workflow Tool connected to your agent, with this sub-workflow:

When Executed by Another Workflow → HTTP Request → Code

Keep your existing HTTP request settings. Set the Code node to “Run Once for All Items” and use:

const products = $input.first().json._embedded.items;

return [{
  json: {
    items: products.map(product => ({
      sku: product.values.sku?.[0]?.data ?? null,
      libelle_240: product.values.libelle_240?.[0]?.data ?? null,
    })),
  },
}];

Publish that sub-workflow and select it in the tool. The agent gets only these two fields for every returned product, without receiving the full API response first.

@flipflip the standard "Optimizec Response " feature in the n8n Request node currently does not support array wildcards (like items[*] or items) to pluck nested values dynamically

Since you explicitly hardcorded the index item[0] , n8n limits the filtered scope to strictly that first entry . To extract specific values accross every object inside the array dynamically while keeping payloads lean for an LLM agent , you can apply two reliable solutions:

Solution 1 : Use a JavaScript Expression ( Recommended)

You can leaverage JavaScript’s Built-in .map() and .pluck() array operators inside n8n expression to dynamically map over the array and filter just the fields you want.

  1. Next to Fields parameter box, switch the toggle from fixed to expression.

  2. Paste the following mapping expression:

{{$json._embedded.items.map(item=> ({sku: item.values.sku[0].data,  libelle: item.values.libelle_240[0].data})) }}

This extracts the dynamic attributes from every item in your _embedded.items array and bundles them into a clean array structure for your LLM.

Solution 2 :Pre-process with an "Edit Fields (set) "Node
if you prefer configuring it natively via workflow paths before passing the schema data to your Agent tool, use an Edit Fields node right after your API Call.

  1. Add an Edit Fields (set) node after the HTTP Request node.

  2. Toggle off keep All other fields (to remove the bulky raw data metadata).

  3. Click Add Field , name it optimized_items, select Array as the data type, and use this expression below :

{{$json._embedded.items.pluck('values')}}

Alternatively , map it cleanly via a small JavaScript snippet inside a Code Node if you want perfect precision over the nested key targets:

return _embedded.items.map(i => {
  return { 
       sku: i.values.sku[0].data,
       libelle: i.values.libelle_240[0].data
  };
});

Hello,

I just tested with

{{$json._embedded.items.map(item=> ({sku: item.values.sku[0].data, libelle: item.values.libelle_240[0].data})) }}

And I got the message

fields is not iterable

From what I understand, $json is to be used for processing the input, whereas in my case it’s filtering the output values.

Philippe.

Perfect, it works with your solution.

Thanks.