Start a Scenario with Json

Hi there
I was wondering how I can start a scenario with the following JSON

[
{
	"code": 886079657,
	"code_client": "1377",
	"name": "ACME",
	"tags": [
		{"tag": "suplier"},
		{"tag": "test2"},
		{"tag": "test1"},
		{"tag": "test3"}
	],
"contry": "EUA",
]

And on a second function insert a code like it

const onlyTags = data.map(f => f.tags.map(t => `VALUE: ${t.tag}`))
onlyTags.push(data.code_client.lenght <= 3 ? "VALUE: 'lower number'" : "VALUE: 'Great number'")

Hi @tuliovargas,

In general you can run any function you like using the Function Node and keeping the n8n data structure in mind.

I am just not sure I fully understand the task you are facing. Do you want to replace the original item (or possibly multiple items with the same structure) with a single item only having the onlyTags field? Or do you want to add the onlyTags field to your existing item?

Hi @MutedJam ,
please, have a look in the code below, I hope it can explain what I want to do:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "value": "[\n{\n\t\"code\": 886079657,\n\t\"code_client\": \"1377\",\n\t\"name\": \"ACME\",\n\t\"tags\": [\n\t\t{\"tag\": \"suplier\"},\n\t\t{\"tag\": \"test2\"},\n\t\t{\"tag\": \"test1\"},\n\t\t{\"tag\": \"test3\"}\n\t],\n\"contry\": \"EUA\",\n]"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const data = items[0].json\nconst onlyTags = data.map(f => f.tags.map(t => `VALUE: ${t.tag}`))\nonlyTags.push(data.code_client.lenght <= 3 ? \"VALUE: 'lower number'\" : \"VALUE: 'Great number'\")\n\nreturn onlyTags;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

That is not possible with a Set-Node (at least not easily, clean and simple). To do that you would have to use a Function-Node with the following code:

return [{
  json: {
    propertyName: [
      {
        "code": 886079657,
        "code_client": "1377",
        "name": "ACME",
        "tags": [
          {"tag": "suplier"},
          {"tag": "test2"},
          {"tag": "test1"},
          {"tag": "test3"}
        ],
        "contry": "EUA",
      }
    ]
  }
}]
1 Like