I want to create json multiple row

Hi, I use function node to build json with many rows

I don’t know how with “json” text appear in it.
How do I make it true?

This code can not extract 2 row:

return [
  {
    json: {
    title: "bai 01",
    request: "note 01"
    },
    {
    title: "bai 02",
    request: "note 02"
    }
  }
]

Sorry, I do not understand what you mean with “How do I make it true?”

The documentation of the Function-Node can be found here:
https://docs.n8n.io/nodes/n8n-nodes-base.function/#function

And information about the internal n8n data structure can be found here:
https://docs.n8n.io/reference/data/data-structure.html#data-structure

Hi @cmdntd987! Maybe this code snippet can help you achieve what you seek? :slight_smile:

return [{
    json: {
      title: "bai 01",
      request: "note 01"
    }
  },
  {
    json: {
      title: "bai 02",
      request: "note 02"
    }
  }
];

This will cause the data to appear in two different rows.

Hope that helps!

I have error like this:

Yes. That 's exactly what I need

1 Like

Glad to hear that worked out @cmdntd987! I’d also take a peek at the resources @jan mentioned above to gain deeper insights on how the Function node works as well as what the internal data structure in n8n looks like. Have fun! :slight_smile:

1 Like

Hi, I want to extract multi rows from this loop

const json = items[0].json["data"];

var count = json.count;

var subject = [];
for (i = 0; i < count; i++) {
  subject[i] = json["subject__" + (i + 1)];
}

How could I convert to json with structure like above

return [{
    json: {
      subject__1: "bai 01"
    }
  },
  {
    json: {
      subject__2: "bai 02"
    }
  }
]

Hi @cmdntd987, I am not a hundred percent certain about what exactly you are trying to achieve since I don’t have enough information to go on but maybe something like the following workflow helps?

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const newItems = [];\n\nfor(let i=0;i<items.length;i++) {\n  newItems.push({json: {subject: \"subject__\" + (i+1), title: items[i].json.title}});\n}\n\nreturn newItems;\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{\n    json: {\n      title: \"bai 01\",\n      request: \"note 01\"\n    }\n  },\n  {\n    json: {\n      title: \"bai 02\",\n      request: \"note 02\"\n    }\n  }\n];"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function1": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi, my problem is more complex
Json field does not in the same column. So I try to loop to re-create variable.

You see my data structure:
(“count” is [i] for loop)

[{ "data":
{ "use_for": "Website",
  "fresh": [ "100% viết mới, không copy", "Image", "Custom" ], 
  "subject__1": "bài 1", 
  "length__1": "400", 
  "format__1": [ "SEO", "PR", "Review", "Ebook" ], 
  "subject__2": "bài 2", 
  "length__2": "400", 
  "format__2": "", 
  "subject__3": "bài 3", 
  "length__3": "400", 
  "format__3": [ "SEO", "PR" ], 
  "custom": "Test ghi chú", 
  "your-name": "My name", 
  "your-email": "name@test.net", 
  "your-phone": "012345678", 
  "count": "3", 
  "price": "400000" }
}]

Could I use this?

newItems.push({json: {subject: "subject__" + (i+1), title: items[i].json["subject__" + (i+1)]}});

Well, I 've done it

By your function, and catch by it name
Thank you so much

const json = items[0].json["data"];

var count = json.count;

const newItems = [];
for(let i=0;i<count;i++) {
  newItems.push({json: {subject: "subject__" + (i+1), title: json["subject__" + (i+1)]}});
}

return newItems;

I have a issue

When I try to extract “format” data, it extract array text:

[“SEO”, “PR”, “Review”, “Ebook”]

How could I extract just value like:

SEO, PR, Review, Ebook

I run this loop to extract, and it seems ok

  var format_array = data["format__" + (i+1)];
  var format;
  format_array.forEach(function(entry) {
    format += entry + ", "
  });

But there is something wrong at first of loop?

undefinedSEO, PR, Review, Ebook,

Hi @cmdntd987! Inside the loop, you can use the array.push() function to add items into the array (as we did in the example above). You can find out more about how to use that here:

I 've done
I extract like array, and “join” its elements

Hey @cmdntd987, did it work out? :slight_smile:

It worked!
I could join elements by “join function”
Its result is beautiful, because of lack of last comma

2 Likes

Glad to hear that. Have fun! :slight_smile: