Ways to get values form function

Hi folks,
I’d like to know how many ways we can get the values from a function:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -1220,
        900
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n\"fruits\": [\n    {\n      \"name\": \"grape\",\n      \"price\": \"2.2\",\n      \"amout\": \"99\"\n    },\n    {\n      \"name\": \"grape\",\n      \"price\": \"2.2\",\n      \"amout\": \"99\"\n    }\n  ],\n  \"city\": \"New York\",\n  \"country\": \"usa\",\n  \"emails\": [\n    \"[email protected]\",\n    \"[email protected]\"\n  ],\n  \"address_delivery\": {\n    \"street\": \"St bluu 90\",\n    \"city\": \"Orlando\",\n    \"state\": \"FL\",\n    \"country\": \"EUA\"\n  },\n  \"total_value\": \"123546.90\",\n  \"status\": \"paid\"\n    }\n\n}]\n"
      },
      "name": "values_json",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -1010,
        980
      ]
    },
    {
      "parameters": {
        "functionCode": "const grape = items[0].json.fruits.name\nconst grape2 = $node[\"values_json\"].json[\"fruits\"][0][\"name\"]\nconst grape_amount = \n\n//pupulate array with all fruits\nconst fruit2 = []\n\nconst city = \n\nconst delivery = \n\nreturn items;"
      },
      "name": "getValues",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -800,
        980
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "values_json",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "values_json": {
      "main": [
        [
          {
            "node": "getValues",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hey @tuliovargas,

That looks like a rather neat image you have put together, is it from a training program somewhere?

If you do console.log(items[0].json); and check the browser dev console when running it will show you the objects you can use and should help you find the answer.

This might be helpful.

There is a website called http://jsonselector.com

Which lets you to generate selectors visually from the JSON. So you can get idea about how to pick the child objects.

Your function node should look something like:

const data = items[0].json;

const grape = data.fruits[0].name
const grape2 = data.fruits[1].name

const reducer = (previousValue, currentValue) => previousValue + parseInt(currentValue.amout, 10);

const grape_amount = data.fruits.reduce(reducer, 0)

//pupulate array with all fruits
const fruits = data.fruits;

const city = data.city

const delivery =  data.delivery

return [
  {
    json: {
      grape,
      grape2,
      grape_amount,
      fruits,
      city,
      delivery
    }
  }  
]
Example workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -10,
        270
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n\"fruits\": [\n    {\n      \"name\": \"grape\",\n      \"price\": \"2.2\",\n      \"amout\": \"99\"\n    },\n    {\n      \"name\": \"grape\",\n      \"price\": \"2.2\",\n      \"amout\": \"99\"\n    }\n  ],\n  \"city\": \"New York\",\n  \"country\": \"usa\",\n  \"emails\": [\n    \"[email protected]\",\n    \"[email protected]\"\n  ],\n  \"address_delivery\": {\n    \"street\": \"St bluu 90\",\n    \"city\": \"Orlando\",\n    \"state\": \"FL\",\n    \"country\": \"EUA\"\n  },\n  \"total_value\": \"123546.90\",\n  \"status\": \"paid\"\n    }\n\n}]\n"
      },
      "name": "values_json",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        220,
        270
      ]
    },
    {
      "parameters": {
        "functionCode": "const data = items[0].json;\n\nconst grape = data.fruits[0].name\nconst grape2 = data.fruits[1].name\n\nconst reducer = (previousValue, currentValue) => previousValue + parseInt(currentValue.amout, 10);\n\nconst grape_amount = data.fruits.reduce(reducer, 0)\n\n//pupulate array with all fruits\nconst fruits = data.fruits;\n\nconst city = data.city\n\nconst delivery =  data.delivery\n\nreturn [\n  {\n    json: {\n      grape,\n      grape2,\n      grape_amount,\n      fruits,\n      city,\n      delivery\n    }\n  }  \n]"
      },
      "name": "getValues",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        430,
        270
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "values_json",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "values_json": {
      "main": [
        [
          {
            "node": "getValues",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi @tuliovargas,

If you’re looking for all the different ways you can reference an input field in the function node, I’d point you towards the expressions documentation — everything there can be used in the function node too.