Find right object

Hi

I have an issue to find out how i could map a certain data object from a (array/list) in a json to a object in next node. I would like to find the right object value based on the object id. So i would in below example like to set a objectvalue in a second node that have “objectId”. I hope you understand.

"evetData":{

:"Objects":[
{
"objectId": "object1",
"objectvalue": "10"
},
{
"objectId": "object2",
"objectvalue": "20"
}
]
}


"evetData":{
:"Objects":[
{
"objectId": "object3",
"objectvalue": "22"
},
{
"objectId": "object2",
"objectvalue": "20"
},
{
"objectId": "object1",
"objectvalue": "10"

]
}

I would like to populate a property

Next node

{
  "recivedobjectValue": "10" // from value with "Object1"
}

Hey, @Mattias_Larsson

Not sure If I understood well but I hope the example below helps. If not simply get back to me.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n   {\n      json: {\n          evetData: {\n             Objects: [\n                 {\n                   objectId: \"object3\",\n                   objectvalue: \"23\"\n                  },\n                  {\n                   objectId: \"object2\",\n                   objectvalue: \"20\"\n                  },\n                  {\n                   objectId: \"object1\",\n                   objectvalue: \"10\"\n                  }\n             ]\n          }\n      }\n   }\n]"
      },
      "name": "mockup",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        480,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const elements = items[0].json.evetData.Objects;\n\nconst results = [];\n\nfor (const element of elements) {\n  results.push({ json: { ...element } })\n}\n\nreturn results;\n\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        670,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "mockup",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "mockup": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Also I am not sure how your data looks like exactly. If you have the data in different items instead of one (like @RicardoE105 solution) something like that would be possible:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      \"evetData\":{\n        \"Objects\":[\n          {\n            \"objectId\": \"object1\",\n            \"objectvalue\": \"10\"\n          },\n          {\n            \"objectId\": \"object2\",\n            \"objectvalue\": \"20\"\n          }\n        ]\n      }\n    }\n  },\n  {\n    json: {\n      \"evetData\":{\n        \"Objects\":[\n          {\n            \"objectId\": \"object3\",\n            \"objectvalue\": \"22\"\n          },\n          {\n            \"objectId\": \"object2\",\n            \"objectvalue\": \"20\"\n          },\n          {\n            \"objectId\": \"object1\",\n            \"objectvalue\": \"100\"\n          }\n        ]\n      }\n    }\n  },\n]"
      },
      "name": "MockData",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "recivedobjectValue",
              "value": "={{$node[\"GetValue\"].json[\"recivedobjectValue\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return {\n  recivedobjectValue: item[\"evetData\"][\"Objects\"].find(data => data.objectId === 'object1').objectvalue,\n}\n"
      },
      "name": "GetValue",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "MockData": {
      "main": [
        [
          {
            "node": "GetValue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GetValue": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Theoretically could you also do the find directly in an expression on the parameter but it seems like that it parses something wrong. Because if I do that I always get the error message: Malformed arrow function parameter list).
So an additional step via a Function-Nodes like either @RicardoE105 or I did is sadly needed.

Thank you, will try it out.

Kind regards
Mattias

1 Like

Thanks for the help i did try it out but i realise that i forgot to add one thing

I would like to map 3 object in the second node but there is only data for 2 of them in the json. can i still make the node process the 2 that gets data but ignore last one.

  "nodes": [
    {
      "parameters": {
        "functionCode": "return {\n  atd: item[\"evetData\"][\"Objects\"].find(data => data.objectId === 'object1').objectvalue,\n  etd: item[\"evetData\"][\"Objects\"].find(data => data.objectId === 'object2').objectvalue,\n  etx: item[\"evetData\"][\"Objects\"].find(data => data.objectId === 'object19').objectvalue\n  \n  \n}\n"
      },
      "name": "GetValue",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        650,
        450
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      \"evetData\":{\n        \"Objects\":[\n          {\n            \"objectId\": \"object1\",\n            \"objectvalue\": \"10\"\n          },\n          {\n            \"objectId\": \"object2\",\n            \"objectvalue\": \"20\"\n          }\n        ]\n      }\n    }\n  },\n  {\n    json: {\n      \"evetData\":{\n        \"Objects\":[\n          {\n            \"objectId\": \"object3\",\n            \"objectvalue\": \"22\"\n          },\n          {\n            \"objectId\": \"object2\",\n            \"objectvalue\": \"20\"\n          },\n          {\n            \"objectId\": \"object1\",\n            \"objectvalue\": \"100\"\n          }\n        ]\n      }\n    }\n  },\n]"
      },
      "name": "MockData",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        450
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "recivedobjectValue",
              "value": "={{$node[\"GetValue\"].json[\"recivedobjectValue\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        850,
        450
      ]
    }
  ],
  "connections": {
    "GetValue": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MockData": {
      "main": [
        [
          {
            "node": "GetValue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

By the way i hope i made right now with code added to the post. 

We are starting to get more and more of the concept struggling with some of the code in the nodes. But like i said many times before it is a great software.

Hey @Mattias_Larsson If I understood well the example below should work

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const response = {};\n\nconst objects = item.eventData.Objects\n\nfor (const object of objects) {\n    console.log(object)\n    if (object.objectId === 'object1') {\n      response.atd = object.objectValue;\n    }\n  \n    if (object.objectId === 'object2') {\n      response.etd = object.objectValue;\n    }\n      \n    if (object.objectId === 'object19') {\n      response.etx = object.objectValue;\n    }\n}\n\nreturn response;\n\n"
      },
      "name": "GetValue",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        730,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      \"eventData\":{\n        \"Objects\":[\n          {\n            \"objectId\": \"object1\",\n            \"objectValue\": \"10\"\n          },\n          {\n            \"objectId\": \"object2\",\n            \"objectValue\": \"20\"\n          }\n        ]\n      }\n    }\n  },\n  {\n    json: {\n      \"eventData\":{\n        \"Objects\":[\n          {\n            \"objectId\": \"object3\",\n            \"objectValue\": \"22\"\n          },\n          {\n            \"objectId\": \"object2\",\n            \"objectValue\": \"20\"\n          },\n          {\n            \"objectId\": \"object1\",\n            \"objectValue\": \"100\"\n          }\n        ]\n      }\n    }\n  },\n]"
      },
      "name": "MockData",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        500,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "MockData",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MockData": {
      "main": [
        [
          {
            "node": "GetValue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
2 Likes