Hi, I try to count rows of json from “set node”
Which best solution for me to count rows of json by javascript function?
I usually used this to get an element:
items[0].json["email"]
But don’t know how to get all elements to count or length it?
Hi, I try to count rows of json from “set node”
Which best solution for me to count rows of json by javascript function?
I usually used this to get an element:
items[0].json["email"]
But don’t know how to get all elements to count or length it?
@cmdntd987 assuming ‘email’ is an array you can do items[0].json["email"].length
items[0] is just catch first row
I want to count rows in json, not elements in first row
@cmdntd987 can you share with me an example of the data (json) and what field you want to count. I can help better that way.
@cmdntd987 ahh got it. The example below should do it.
{
"nodes": [
{
"parameters": {},
"name": "Start",
"type": "n8n-nodes-base.start",
"typeVersion": 1,
"position": [
250,
300
]
},
{
"parameters": {
"functionCode": "for (let i = 0; i < items.length; i++) {\n const data = items[i].json\n const elementCount = Object.keys(data).length\n items[i].json['count'] = elementCount\n}\n\nreturn items;"
},
"name": "Function",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
670,
300
],
"notesInFlow": true,
"notes": "Count function"
},
{
"parameters": {
"functionCode": "return [\n {\n json: {\n element1: 1,\n element2: 2,\n element3: 3,\n } \n },\n {\n json: {\n element1: 1,\n element2: 2,\n }\n }\n]"
},
"name": "Function1",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
460,
300
],
"notesInFlow": true,
"notes": "Data mockup"
}
],
"connections": {
"Start": {
"main": [
[
{
"node": "Function1",
"type": "main",
"index": 0
}
]
]
},
"Function1": {
"main": [
[
{
"node": "Function",
"type": "main",
"index": 0
}
]
]
}
}
}
Yeah. It save my life
var length = items.length;
return [{
json: {length: length}
}]