Remove Parent Field in JSON Response

Hi,

Ive been trying to make a HTTP Request which gives me particular contacts. The issue with this is that I want the contacts to in each row in the table but here its not getting due to the parent field contact. Is there a function with which we can remove the parent field and get list of contacts in the table. Also I do not need has-more vid-offset in the response.

Thanks

You can use a function node for that. Check the example below:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://mockup-pj5l0yxsjrbr.runkit.sh/",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const results = []\n\nfor (const item of items[0].json.contacts) {\n\n      results.push({\n        json: item\n      })\n    \n}\n\nreturn results;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        710,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
2 Likes

Thanks a lot for this @RicardoE105 :grinning:

1 Like

Hi @RicardoE105, I have a similar request but I am running into an issue with the function.

I am using the HTTP Request node to just like Asi_Joshi did but the API response contains nested arrays in my case. As an example, instead of Contacts, I have user-data or session-data and a preceding column in the table with a single timestamp that I want to skip.

Here is the updated function code I used.

const results = []

for (const item of items[0].json.session-data) {

  results.push({
    json: item
  })

}

return results;

I am getting the following error.

ERROR: data is not defined

Thanks!

Welcome to the community @Pier-Andre_Maynard!

You would have to change the above to items[0].json["session-data"] else it wants to deduct whatever value is in data (in this case it does not exist, that is why it errors) from items[0].json.session.

1 Like

Thanks @jan! This works great!

On a side-note/topic, is there a recommended approach to handle nested arrays within the data? (I should probably do another forum search first)

For my use case, this is for scheduling sessions and I have multiple speakers/presenters in an array with their details for some of the sessions.

Probably something like this JSON nested arrays, Function and Split In Batches (haven’t tested yet with my own data).

Thanks again!

So you want to get each of the speakers/presenters of all of the events into an own item? How does the data look like exactly?

Hi @jan, thanks for the guidance. What I am trying to achieve is a sync between 2 systems. The first one is our source of truth, has the initial REST API and the second one is using GraphQL.

For any session, you would have the scheduling information, title, description, metadata and attached/assigned speakers. I am not sure yet if I can do this in a single GraphQL call to recreate that session in the 2nd system or if I need to create the session first then a 2nd call to assign the speakers.

The default output of the rest API is in XML but can be switched to JSON with a simple parameter in the request.
You can find the data structure in the attached screenshot, the fields will vary a bit from session to session, there is a toggle in their backend to output or not the fields when they have blank values.

Thank you!

We have released the Item Lists node that allows one to split the items without the need of writing code. If the data has the following structure (similar to the original post)

, you can use the below workflow

If your data is nested within a different key, enter that key in the Field To Split Out filed in the Item Lists node.

1 Like