Discard top level in json array returned by HTTP request

I currently have a json array returned by a HTTP request of the form:

[
 {
  "data": {
   "key1": 2,
   "key2": "Twenty",
   "key3": {
    "subkey1": "Something"
    }
   }
 },
 {
  "data": {
   "key1": 49,
   "key2": "TwentyTwo",
   "key3": {
    "subkey1": "Somethingelse"
   }
  }
 }
]

I’d like to discard the first “data” level and retain only deeper levels (i.e. key1, key2, key3). This would look something like the following:

[
 {
   "key1": 2,
   "key2": "Twenty",
   "key3": {
    "subkey1": "Something"
   }
 },
 {
   "key1": 49,
   "key2": "TwentyTwo",
   "key3": {
    "subkey1": "Somethingelse"
  }
 }
]

Does anyone have any comments for how I can use the function node to achieve this?

Example mock input node:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n{\n  json: {\n    \"data\": {\n      \"key1\": 2,\n      \"key2\": \"Twenty\",\n      \"key3\": {\"subkey1\": \"Something\"}\n    }\n  }\n},\n{\n  json: {\n    \"data\": {\n      \"key1\": 49,\n      \"key2\": \"TwentyTwo\",\n      \"key3\": {\"subkey1\": \"Somethingelse\"}\n    }\n  }\n}]\n"
      },
      "name": "MockInput",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        830,
        -60
      ],
      "notesInFlow": true,
      "notes": "Mockup data"
    }
  ],
  "connections": {}
}

Hey @gordo!

Here’s the function that you can use:

return $items().map(item => {
  const value = item.json.data;
  return {
    json: value
  }
});

Perfect. That did the trick. I need to learn more about mapping.

1 Like