API JSON search for name and get the value

Team,
Have another struggle.
I do an HTTP Get function to pull all available customfiled from an account. {GET customfildid}

I am looking for the ID of each customfield to do the HTTP POST on the following node. {POST the Lead}

The struggle I am having is that the arrangement of the result will change from one account to another account. So for example on one account name=locationid will be arranged as the first one another account name=locationid will be second, and If I select in the expression the “ID” one time it might be “locationid” another might be “Age”

Basically, I guess the best solution is to search the JASON for the NAME and get its current ID and do this for each field I need. After that send these values to {POST the Lead}

With regret, I have no idea how to achieve this and hope someone can help.

Thank you for everything.

We were able to write something like that and looks like get us the info, but maybe there are an easier solution :slight_smile:

let i = 0;
for (item of items) {
  let arg0 = $node["GET customfildid"].json["customFields"][i]["name"];
  if(arg0 = "county"){
    a = $node["GET customfildid"].json["customFields"][i]["id"];
     item.json.county = $node["GET customfildid"].json["customFields"][i]["id"];
     i++;
  }
    if(arg0 = "age"){
    a = $node["GET customfildid"].json["customFields"][i]["id"];
     item.json.age = $node["GET customfildid"].json["customFields"][i]["id"];
     i++;
  }
  //if(arg0="Lead Type")
}
return items;

Hi @kgurinov, I understand you have an item like this (with a customFields array where name has the name of each field and id has the value of each field):

{
    "customFields": [
        {
            "name": "county",
            "id": "foo"
        }, {
            "name": "age",
            "id": "bar"
        }, {
            "name": "Lead Type",
            "id": "baz"
        }
    ]
}

If so, you could simplify the process of moving each object of customFields into their own field a bit and use a snippet like this in a Function node:

for (item of items) {
  for (customField of item.json.customFields) {
    item.json[customField.name] = customField.id;
  }
}

return items;

This would save you from having to specify each individual field you’re looking for. Simply connect the Function node right after a node returning customFields. Here is a dummy workflow showing this in action. You can simply copy the code and paste it in your n8n canvas.

Example Workflow using Function

To further simplify your code you could use the Function Item node instead of Function. This way, you wouldn’t need to iterate through all incoming items and can simplify the above code a bit more.

for (customField of item.customFields) {
    item[customField.name] = customField.id;
}
return item;
Example Workflow using Function Item

Hope this helps! Let me know if you have any questions on this.