JSON Array to File

Hello I’m having a JSON array and i need to write to a file. I’m using Move Binary Data, but only one item is being writed.
JSON example below.

vvcap 2021-04-05-08-34-10

For example like this jode but for all items.

return items[0].json.aggregations.sources["buckets"].map(item => { 
  return { 
    json: {
      src_ip: item,
    }
  }
});

I do understand the problem but not how the code you did post is related to it but I do not think it matters looking at the first screenshot.

Back to the problem. Do I understand you correctly thay you want to write a JSON-file which contains an array?

That is correct, the code is not related at all, it’s just the example “Make Binary” to convert an item to binary and then write to a file. Yes it is correct, the first photo is my array, each item has nested json but there is not “main key”, heres an example of each item. I need each item to be writed to a file.

Hm, so then looking at your first screenshot. How many files do you expect. Would you expect 11 files or 341 (11x31)?

I do not expect any files, that array that i’m showing i need to be writed to a single file. The length could be 11 or 7 (it’s unpredictable). I just need to write a JSON Array to a file.

OK, then you lost me because you wrote now three different things (no file, one file, multiple files). Because between “I need each item to be writed to a file.” and “I do not expect any files, that array that i’m showing i need to be writed to a single file” is a big difference. Is it now a single file that contains an array or is it a file per item?

Please post a workflow example that contains a node that outputs exactly the data you have currently in the last node that you want to write (obviously the values of that objects can be different if it is sensitive data).
Additionally, also post a JSON-example with exactly the same data of how that file (or one of the multiple ones) you want to end up, should look like. To be 100% sure also tell me how many files you expect as output. Once you posted exactly that, I will have another look.

I understand, my mistake. The first photo shows the data on my last node (A Cortex Output, an array of JSON), the second photo shows an example of the data of each item (there’s more data but it can’t fit the screen). I expect only one file containing all the previous (for example, all 11 items).
vvcap 2021-04-05-08-34-10

Still not sure if the screenshot shows all the data or only one key which contains an array but for now assume it is all the data. In this case the following can be done:

With the following code in the Function-Node named “Convert” as in this example.

return [
  {
    json: {
      data: items.map(item => item.json)
    }
  }
]

Here the full workflow:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      a: 1\n    }\n  },\n  {\n    json: {\n      b: 2\n    }\n  }\n];"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "mode": "jsonToBinary",
        "convertAllData": false,
        "options": {}
      },
      "name": "Move Binary Data",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "fileName": "example.json"
      },
      "name": "Write Binary File",
      "type": "n8n-nodes-base.writeBinaryFile",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      data: items.map(item => item.json)\n    }\n  }\n]\n \n"
      },
      "name": "Convert",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Convert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Move Binary Data": {
      "main": [
        [
          {
            "node": "Write Binary File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert": {
      "main": [
        [
          {
            "node": "Move Binary Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

That’s exactly what i wanted, Thanks! In this cases i’m kind of lost that why i’m so dependent on this forum. It will be nice to have a guide on how to handle arrays, because some nodes it relays on using items[0] as reference but in this particular case when it’s a plain array it’s kind of confusing on how to use certain nodes.