Add variable with function node

Hello,
I have a function node that return data:
n8n

then, I have an HTTP node wich get the three requests and return looks like:

[
	{
		"order_detail": {
			"id": 106,
			...some other data
		}
	},
		{
		"order_detail": {
			"id": 107,
			...some other data
		}
	},
		{
			"order_detail": {
			"id": 108,
			...some other data
		}
	}
]

I would like to add variable into each order_detail to have something like;

[
	{
		"order_detail": {
			"id": 106,
 			"myVariable": 123,
			...some other data
		}
	},
		{
		"order_detail": {
			"id": 107,
 			"myVariable": 123,
			...some other data
		}
	},
		{
			"order_detail": {
			"id": 108,
 			"myVariable": 123,
			...some other data
		}
	}
]

Just to be sure that I understand you correctly. The Function-Node and its data do actually not matter at all. All you want to do is to add an identical key to each of the order_detail objects that you get as result of the HTTP Request Node?

yes, that’s right.

Then you can use a Function-Node with this code:

return items.map(item => {
  item.json.order_detail.myVariable = 123;
  return item;
});

Here the example workflow:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return this.helpers.returnJsonArray([\n\t{\n\t\t\"order_detail\": {\n\t\t\t\"id\": 106,\n\t\t}\n\t},\n\t\t{\n\t\t\"order_detail\": {\n\t\t\t\"id\": 107,\n\t\t}\n\t},\n\t\t{\n\t\t\t\"order_detail\": {\n\t\t\t\"id\": 108,\n\t\t}\n\t}\n]);"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return items.map(item => {\n  item.json.order_detail.myVariable = 123;\n  return item;\n});"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        700,
        300
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thank you @jan
At the opposite, if I want to filter a data and filter every “myVariable: 123” ?

Not sure I understand. Do you want to remove all items which do not have “myVariable” set to 123?

I would like to remove all items “myVariable”.

I assume you mean with ‘item’ -> 'property`. In this case the function would look like this:

return items.map(item => {
  delete item.json.order_detail.myVariable;
  return item;
});

Here the example workflow:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return this.helpers.returnJsonArray([\n\t{\n\t\t\"order_detail\": {\n\t\t\t\"id\": 106,\n\t\t}\n\t},\n\t\t{\n\t\t\"order_detail\": {\n\t\t\t\"id\": 107,\n\t\t}\n\t},\n\t\t{\n\t\t\t\"order_detail\": {\n\t\t\t\"id\": 108,\n\t\t}\n\t}\n]);"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return items.map(item => {\n  item.json.order_detail.myVariable = 123;\n  return item;\n});"
      },
      "name": "Add Property",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return items.map(item => {\n  delete item.json.order_detail.myVariable;\n  return item;\n});"
      },
      "name": "Remove Property",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Add Property",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add Property": {
      "main": [
        [
          {
            "node": "Remove Property",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

thanks a lot.
One last thing (I hope…), how unserialize the data? So start with:

[
	{
		"order_detail": {
			...some data
		}
	},
		{
		"order_detail": {
			...some  data
		}
	},
		{
			"order_detail": {
			...some data
		}
	}
]

and ave this return:

{
	"order_detail": {
		...some data
	}
},
	{
	"order_detail": {
		...some  data
	}
},
	{
		"order_detail": {
		...some data
	}
}

Your output is not valid Javascript Object Notation

Sorry I don’t understand why?

Because the first thing represents an Array. The second is neither an Array nor an Object.

Ok, so to convert data with input like:

    [
	{
		"order_detail": {
			...some data
		}
	},
	]

To output like:

{
    "order_detail": {
    			...some data
    		}
},
return items[0]

Ok, so simple… thank you.