Inserting Data into existing JSON

Describe the issue/error/question

I prepped a “template” file, that I load in via Binary Read and need to append some data into a certain spot.

My Json looks like this

{
    "Categories": [
      {
        "name": "Default",
        "name": "Another Category"
      }
    ],
    "Data": [NEED DATA HERE]
}

The data that I need is being grabbed via a HTTP GET and then formatted, that part has no issues. The result is similar to the following (just with around 100 entries).

[
  {
  "category":"Default",
  "url":"https://example.com/file.png",
  "description":"string",
  "user":"string",
  "userAvatar":"https://example.com/file.png"
  },
]

so the expected result should be:

{
    "Categories": [
      {
        "name": "Default",
        "name": "Another Category"
      }
    ],
    "Data": [
          {
          "category":"Default",
          "url":"https://example.com/file.png",
          "description":"string",
          "user":"string",
          "userAvatar":"https://example.com/file.png"
          },
          {
          "category":"Default",
          "url":"https://example.com/file.png",
          "description":"string",
          "user":"string",
          "userAvatar":"https://example.com/file.png"
          }
    ]
}

I seem to have some misunderstandings on how to merge data, hope you can help.

Information on your n8n setup

  • n8n version:You’re on 0.202.1
  • Database SQLite
  • Running n8n with the execution process own
  • Running n8n via Docker

Hi @aleks, welcome to the community :tada:

The merge node would merge individual items, but it seems your HTTP Request is already returning multiple items which you would like to treat as one item.

So, this will require a little bit of custom code I think. For example like so:

You can see that here I am first returning the template on my Mock template node. This would be the lower branch from your screenshot. I am then making an HTTP Request which would be the upper branch in your screenshot.

Lastly, I am running this bit of JS code:

const template = $('Mock template').first().json;

template.Data = $input.all().map(e => e.json);

return [{
  json: template
}];

This code reads the first (and only) element from the Mock template node.

It then puts all the incoming items under the Data key of your template and removes their json key. This key is used by n8n to distinguish different data types, but you most likely don’t want that in your output data.

It then returns the result.

Hope this helps! Let me know if you have any questions on this :slight_smile:

Interesting solution! Thanks for the advice, learned something new today

1 Like

Glad to hear this helps! If you’d like to learn more about the variables and methods I’ve used here, check out this page in our docs: Built in methods and variables reference - n8n Documentation. These will also work in expressions outside of the Code node and can be quite useful.

1 Like

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