Parsing JSON array to extract single element

I have a JSON object that contains an array that’s being returned from an API, and I want to extract only the domain element… what’s the best way to go about it.

[
  {
    "status_code": 200,
    "error": null,
    "response": {
        "scandata_raw": [
        {
          "datasource": "scan",
          "domain": "domain1.com",
          "scan_date": "2024-09-10 08:23:48"
        },
        {
          "datasource": "scan",
          "domain": "domain2.net",
          "scan_date": "2024-09-10 08:23:19"
        },
        {
          "datasource": "scan",
          "domain": "domain3.org",
          "scan_date": "2024-09-10 08:21:06"
        }
      ]
    }
  }
]

I’ve been trying to use a code node to parse out my data, but I don’t know how to access all indices of the array…

Using

return {
  json: {
    domains: $input.all().map(item => item.json.response.scandata_raw)
  }
}

The data gets passed through virtually unchanged.

If I use

return {
  json: {
    domains: $input.all().map(item => item.json.response.scandata_raw[0])
  }
}

I only get the domain at index 0

If I use

return {
  json: {
    domains: $input.all().map(item => item.json.response.scandata_raw.domain)
  }
}

I get no output.

  • n8n version: 1.56.2
  • Database (default: SQLite): Default
  • n8n EXECUTIONS_PROCESS setting (default: own, main): ?
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: macOS

It looks like your topic is missing some important information. Could you provide the following if applicable.

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

Welcome to the community @dmoore44 !

Tip for sharing information

Pasting your n8n workflow


Ensure to copy your n8n workflow and paste it in the code block, that is in between the pairs of triple backticks, which also could be achieved by clicking </> (preformatted text) in the editor and pasting in your workflow.

```
<your workflow>
```

That implies to any JSON output you would like to share with us.


I summarized different approaches in the following workflow

If you are after a single expression that produces domains as an array, you can use

return {
  domains: $input.first().json.response.scandata_raw
    .map(i => {return {domain: i.domain}})
    .map(i => i.domain)
}
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.