Split json to array of items

Describe the problem/error/question

so i have a json that comes as one item
and i need to saperate it to multiply items

What is the error message (if any)?

ERROR: Code doesn’t return items properly
Please return an array of objects, one for each item you would like to output

Please share your workflow

{
“name”: “woocommerce - function input sales process”,
“nodes”: [
{
“parameters”: {},
“id”: “d78b7d94-e15b-46ca-8200-b5d8e8f22e0f”,
“name”: “When clicking "Execute Workflow"”,
“type”: “n8n-nodes-base.manualTrigger”,
“position”: [
-1160,
340
],
“typeVersion”: 1
},
{
“parameters”: {
“jsCode”: “return {\n "BillGoldCompID": "58d6befc-e3d9-4319-b663-3a1d6fcbfcd1",\n "DocType": "1",\n "DocNumber": "23355",\n "ExternalCompID": "1",\n "ResponseCode": "0",\n "InvoiceDefHe.Name": "ע.ו קוברה המלך בע’'מ",\n "InvoiceDefHe.Line1": "תל אביב",\n "InvoiceDefHe.Line2": "טסט 12 מיקוד / ת.ד. 6274401",\n "CashNIS": "0.00",\n "ExtReadInvoiceHead.CustName": "איתמר מועלם",\n "ExtReadInvoiceHead.CustAddresLine1": " הנביא ",\n "ExtReadInvoiceHead.CustCity": "בני ברק",\n "ExtReadInvoiceHead.CustMobilePH": "979597848",\n "ExtReadInvoiceHead.Languge": "HE",\n "ExtReadInvoiceHead.Comments": "מספר הזמנה: 8759 שם: איתמר מועלם",\n "ExtReadInvoiceHead.CoinID": "1",\n "ExtReadInvoiceHead.Email": "[email protected]",\n "ExtReadInvoiceHead.SendByEmail": "True",\n "ExtReadInvoiceHead.CustomerNumber": "0",\n "ExtReadInvoiceHead.AccountId": "0",\n "ExtReadInvoiceHead.Discount": "0.00",\n "ExtReadInvoiceHead.InvoiceDate": "03/05/2023 12/17",\n "ExtReadInvoiceHead.InvoiceType": "1",\n "ExtReadInvoiceHead.InvoiceNumber": "23355",\n "ExtReadInvoiceHead.TerminalNumber": "41242",\n "ExtReadInvoiceHead.TotalIncludeVAT": "149.00",\n "ExtReadInvoiceHead.TotalNoVat": "127.35",\n "ExtReadInvoiceHead.TotalVatFree": "0.00",\n "ExtReadInvoiceHead.VAT": "0.17",\n "ExtReadInvoiceHead.VATOnly": "21.65",\n "ExtReadInvoiceHead.TotalIncludeVATNIS": "149.00",\n "ExtReadInvoiceHead.TotalNoVatNIS": "127.35",\n "ExtReadInvoiceHead.TotalVatFreeNIS": "0.00",\n "ExtReadInvoiceHead.VATOnlyNIS": "21.65",\n "ExtReadInvoiceHead.ISORate": "1.0000",\n "ExtReadInvoiceHead.RoundUp": "0.00",\n "ExtReadInvoiceHead.IsOpen": "False",\n "ExtReadInvoiceHead.DepartmentID": "0",\n "TotalExtReadInvoiceLines": "1",\n "ExtReadInvoiceLines.CostNoVat": "127.35",\n "ExtReadInvoiceLines.Description": "כתיבה היפנוטית",\n "ExtReadInvoiceLines.Discount": "0.00",\n "ExtReadInvoiceLines.InvoiceType": "1",\n "ExtReadInvoiceLines.InvoiceLineNum": "1",\n "ExtReadInvoiceLines.InvoiceNumber": "23355",\n "ExtReadInvoiceLines.IsVatFree": "False",\n "ExtReadInvoiceLines.ProductID": "ea3d9290-a844-4228-bba1-14eb8cbf5bbb",\n "ExtReadInvoiceLines.Quantity": "1.0000",\n "ExtReadInvoiceLines.QuantityD": "1.0000",\n "ExtReadInvoiceLines.TotalLineCostIncludeVAT": "149.00",\n "ExtReadInvoiceLines.TotalLineCostNoVat": "127.35",\n "ExtReadInvoiceLines.TotalLineCostVATOnly": "21.65",\n "ExtReadInvoiceLines.CustomerNumber": "0.1700",\n "ExtReadInvoiceLines.VAT": "0.1700",\n "ExtReadInvoiceLines.DepartmentID": "0",\n "TotalExtShvaParams": "1",\n "ExtShvaParams.CardNumber5": "8769",\n "ExtShvaParams.Status1": "0",\n "ExtShvaParams.Sulac25": "9",\n "ExtShvaParams.JParameter29": "4",\n "ExtShvaParams.Tokef30": "2045",\n "ExtShvaParams.Sum36": "14900",\n "ExtShvaParams.SumStars52": "0",\n "ExtShvaParams.ApprovalNumber71": "0641597",\n "ExtShvaParams.FirstPaymentSum78": "0",\n "ExtShvaParams.ConstPayment86": "0",\n "ExtShvaParams.NumberOfPayments94": "0",\n "ExtShvaParams.AbroadCard119": "0",\n "ExtShvaParams.CardTypeCode60": "3",\n "ExtShvaParams.Mutag24": "8",\n "ExtShvaParams.CardOwnerName": "איתמר מועלם",\n "ExtShvaParams.CardName": "טסט",\n "ExtShvaParams.CreditType63": "1",\n "ExtShvaParams.DealType61": "01",\n "ExtShvaParams.ChargType66": "52",\n "ExtShvaParams.TerminalNumber": "41242",\n "ExtShvaParams.BinId": "0",\n "ExtShvaParams.InternalDealNumber": "139384419",\n "ExtShvaParams.CouponNumber": "00",\n "ExtShvaParams.DealDate": "20230503",\n "ExtShvaParams.CardOwnerPhone": "8578395",\n "ExtShvaParams.HaveRecipient": "true",\n "ExtShvaParams.ExternalPaymentVector": "12",\n "ExtShvaParams.ExternalPaymentID": "118629541",\n "TotalCheque": "0",\n "TotalCustomPay": "0",\n "client": "this ia cobra2 972543489388",\n "system": "CardCom",\n "date_created": "2023-05-03 12:17:00",\n "id": "23355",\n "total": "149.00",\n "order_key": "58d6befc-e3d9-4319-b663-3a1d6fcbfcd1"\n}”
},
“id”: “a55c4561-4259-409d-8832-dfb0cb0a8abc”,
“name”: “woocommerce_dummy”,
“type”: “n8n-nodes-base.code”,
“position”: [
-840,
340
],
“typeVersion”: 1
},
{
“parameters”: {
“values”: {
“number”: [
{
“name”: “date_created”,
“value”: “={{ $json["ExtReadInvoiceHead.InvoiceDate"].split(" ")[0].split("/") [2]}}-{{ $json["ExtReadInvoiceHead.InvoiceDate"].split(" ")[0].split("/") [1]}}-{{ $json["ExtReadInvoiceHead.InvoiceDate"].split(" ")[0].split("/") [0]}} {{ $json["ExtReadInvoiceHead.InvoiceDate"].split(" ")[1] .split("/")[0]}}:{{ $json["ExtReadInvoiceHead.InvoiceDate"].split(" ")[1] .split("/")[1]}}:00”
}
],
“string”: [
{
“name”: “id”,
“value”: “={{ $json.DocNumber }}”
},
{
“name”: “total”,
“value”: “={{ $json["ExtReadInvoiceHead.TotalIncludeVAT"] }}”
},
{
“name”: “client”,
“value”: “={{ $json.client }}”
},
{
“name”: “order_key”,
“value”: “={{ $json.BillGoldCompID }}”
}
]
},
“options”: {}
},
“id”: “f7f41840-b795-43a8-99c3-e5242c0a9c14”,
“name”: “Set”,
“type”: “n8n-nodes-base.set”,
“position”: [
-400,
280
],
“typeVersion”: 1
},
{
“parameters”: {
“operation”: “executeQuery”,
“query”: "=select * from product where product_id = {{ $json["ExtReadInvoiceLines.ProductID"] }} and client=‘{{ $node["set"].json["client"] }}’ "
},
“id”: “f5573323-35d2-4e4d-9b83-d34836fa4238”,
“name”: “MySQL4”,
“type”: “n8n-nodes-base.mySql”,
“position”: [
1340,
280
],
“typeVersion”: 1,
“alwaysOutputData”: true,
“credentials”: {
“mySql”: {
“id”: “4”,
“name”: “Sales DB”
}
}
},
{
“parameters”: {
“conditions”: {
“number”: [
{
“value1”: “={{ $json.id }}”,
“operation”: “isEmpty”
}
]
}
},
“id”: “e2d6a47d-5d0e-4062-bd78-27845880f190”,
“name”: “IF1”,
“type”: “n8n-nodes-base.if”,
“position”: [
1500,
280
],
“typeVersion”: 1
},
{
“parameters”: {
“table”: {
“__rl”: true,
“mode”: “name”,
“value”: “product”
},
“columns”: “product_id,name,sku,price,client”,
“options”: {}
},
“id”: “2012bfa1-9f6c-496f-ba2c-047249cf7c55”,
“name”: “MySQL5”,
“type”: “n8n-nodes-base.mySql”,
“position”: [
1820,
160
],
“typeVersion”: 1,
“credentials”: {
“mySql”: {
“id”: “4”,
“name”: “Sales DB”
}
}
},
{
“parameters”: {
“mode”: “runOnceForEachItem”,
“jsCode”: “const item=$node["Split In Batches"].json;\nlet response = {\n product_id:item["product_id"],\n name:item["name"],\n sku:item["sku"],\n price:item["price"],\n client:$node["Code2"].json["client"]\n}\n\nreturn {json:response};”
},
“id”: “1d6c5470-630d-4db0-9ea0-28ef3046c96e”,
“name”: “Code1”,
“type”: “n8n-nodes-base.code”,
“position”: [
1680,
160
],
“typeVersion”: 1
},
{
“parameters”: {
“batchSize”: 1,
“options”: {}
},
“id”: “3d977947-7970-450a-af00-f1433202db71”,
“name”: “Split In Batches1”,
“type”: “n8n-nodes-base.splitInBatches”,
“position”: [
1180,
280
],
“typeVersion”: 1
},
{
“parameters”: {
“table”: {
“__rl”: true,
“mode”: “name”,
“value”: “sales”
},
“columns”: “order_id,order_key,datetime,total_price,client,meta”,
“options”: {}
},
“id”: “a0f9189f-12c0-4c28-8723-ef162b98ad6b”,
“name”: “MySQL6”,
“type”: “n8n-nodes-base.mySql”,
“position”: [
-40,
280
],
“typeVersion”: 1,
“credentials”: {
“mySql”: {
“id”: “4”,
“name”: “Sales DB”
}
}
},
{
“parameters”: {
“mode”: “runOnceForEachItem”,
“jsCode”: “const item = $node["Split In Batches"].json;\nlet response = {\n order_id: $node["Code2"].json["order_id"],\n product_id: item["product_id"],\n product_name: item["name"],\n quantity: item["quantity"],\n price: item["price"],\n datetime:$node["Code2"].json["datetime"],\n client:$node["Code2"].json["client"]\n}\nreturn {json:response};”
},
“id”: “f132164e-88cd-407b-8346-239c0d89008a”,
“name”: “Code7”,
“type”: “n8n-nodes-base.code”,
“position”: [
2000,
280
],
“typeVersion”: 1
},
{
“parameters”: {
“table”: {
“__rl”: true,
“mode”: “name”,
“value”: “sales_product”
},
“columns”: “order_id,product_id,product_name,quantity,price,datetime,client”,
“options”: {}
},
“id”: “fcabd644-f340-47f9-8587-3363b6adbb21”,
“name”: “MySQL7”,
“type”: “n8n-nodes-base.mySql”,
“position”: [
2180,
280
],
“typeVersion”: 1,
“credentials”: {
“mySql”: {
“id”: “4”,
“name”: “Sales DB”
}
}
},
{
“parameters”: {
“jsCode”: “return $node["Set"].json”
},
“id”: “c31c46f2-9359-45cb-9005-f30f33938f6e”,
“name”: “Code6”,
“type”: “n8n-nodes-base.code”,
“position”: [
340,
280
],
“typeVersion”: 1
},
{
“parameters”: {
“mode”: “runOnceForEachItem”,
“jsCode”: “const item = $node["Set"].json;\nconst response = {\n order_id:item["id"], \n order_key:item["order_key"], \n datetime:item["date_created"], \n total_price:item["total"],\n client:item["client"],\n meta:JSON.stringify(item)\n}\n\nreturn {json:response}”
},
“id”: “17b93f86-c9e2-4dcd-a2d2-1947ffcc6547”,
“name”: “Code5”,
“type”: “n8n-nodes-base.code”,
“position”: [
-220,
280
],
“typeVersion”: 1
},
{
“parameters”: {
“jsCode”: “const input = $node["Code6"].json;\n\nconst output = Object.keys(input).reduce((acc, key) => {\n const parts = key.split(‘.’);\n if (parts.length === 1) {\n acc[key] = input[key];\n } else {\n const objKey = parts.shift();\n const nestedKey = parts.join(‘.’);\n if (!acc[objKey]) {\n acc[objKey] = {};\n }\n acc[objKey][nestedKey] = input[key];\n }\n return acc;\n}, {});\n\nconsole.log(output);\n”
},
“id”: “65dece27-3742-406b-b344-6e523c069e59”,
“name”: “Code15”,
“type”: “n8n-nodes-base.code”,
“position”: [
560,
280
],
“typeVersion”: 1
}
],
“pinData”: {},
“connections”: {
“IF1”: {
“main”: [
[
{
“node”: “Code1”,
“type”: “main”,
“index”: 0
}
],
[
{
“node”: “Code7”,
“type”: “main”,
“index”: 0
}
]
]
},
“Set”: {
“main”: [
[
{
“node”: “Code5”,
“type”: “main”,
“index”: 0
}
]
]
},
“Code1”: {
“main”: [
[
{
“node”: “MySQL5”,
“type”: “main”,
“index”: 0
}
]
]
},
“Code5”: {
“main”: [
[
{
“node”: “MySQL6”,
“type”: “main”,
“index”: 0
}
]
]
},
“Code6”: {
“main”: [
[
{
“node”: “Code15”,
“type”: “main”,
“index”: 0
}
]
]
},
“Code7”: {
“main”: [
[
{
“node”: “MySQL7”,
“type”: “main”,
“index”: 0
}
]
]
},
“Code15”: {
“main”: [
[
{
“node”: “Split In Batches1”,
“type”: “main”,
“index”: 0
}
]
]
},
“MySQL4”: {
“main”: [
[
{
“node”: “IF1”,
“type”: “main”,
“index”: 0
}
]
]
},
“MySQL5”: {
“main”: [
[
{
“node”: “Code7”,
“type”: “main”,
“index”: 0
}
]
]
},
“MySQL6”: {
“main”: [
[
{
“node”: “Code6”,
“type”: “main”,
“index”: 0
}
]
]
},
“Split In Batches1”: {
“main”: [
[
{
“node”: “MySQL4”,
“type”: “main”,
“index”: 0
}
]
]
},
“woocommerce_dummy”: {
“main”: [
[
{
“node”: “Set”,
“type”: “main”,
“index”: 0
}
]
]
},
“When clicking "Execute Workflow"”: {
“main”: [
[
{
“node”: “woocommerce_dummy”,
“type”: “main”,
“index”: 0
}
]
]
}
},
“active”: false,
“settings”: {},
“versionId”: “5f5e5e4f-5cbf-4fbb-9130-5927c435e177”,
“id”: “44”,
“meta”: {
“instanceId”: “c83c08d4687665bc03ba1a0fdc8e1fa8de088080d6d07474213d707c13586195”
},
“tags”: [
{
“createdAt”: “2023-04-19T18:26:24.986Z”,
“updatedAt”: “2023-04-19T18:26:24.986Z”,
“id”: “1”,
“name”: “Grafana”
},
{
“createdAt”: “2023-04-26T21:50:48.677Z”,
“updatedAt”: “2023-04-26T21:50:48.677Z”,
“id”: “3”,
“name”: “Client Income Flow”
}
]
}

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 0.217.2
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (npm):
  • Operating system: ubuntu 20.04

so actually iam trying to get “code 15” to change the json into somthing like this

{
BillGoldCompID: ‘58d6befc-e3d9-4319-b663-3a1d6fcbfcd1’,
DocType: ‘1’,
DocNumber: ‘23355’,
ExternalCompID: ‘1’,
ResponseCode: ‘0’,
InvoiceDefHe: {
Name: ’ קוברה בע''מ’,
Line1: ‘תל אביב’,
Line2: ‘זלטופולסקי 12 מיקוד / ת.ד. 6274401’
},
CashNIS: ‘0.00’,
ExtReadInvoiceHead: {
CustName: ’ מועלם’,
CustAddresLine1: 'הנביא ',
CustCity: ‘city’,
CustMobilePH: ‘773994723’
}
}

You need to return an array of items. like proposed in my answer here: Help with a mock up XML response as $metadata for webhook response - #6 by BramKn

also make sure to put your workflow inside a code block.(preformatted text)

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.