# Merging JSON: inserting array items at the beginning of an existing array

**URL:** <https://community.n8n.io/t/merging-json-inserting-array-items-at-the-beginning-of-an-existing-array/35220>\
**Category:** Questions\
**Tags:** data-transformation\
**Created:** [January 12, 2024, 12:26pm UTC](https://community.n8n.io/t/merging-json-inserting-array-items-at-the-beginning-of-an-existing-array/35220 "2024-01-12T12:26:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tomtom](https://community.n8n.io/u/tomtom)\
**Post date:** [January 12, 2024, 12:26pm UTC](https://community.n8n.io/t/merging-json-inserting-array-items-at-the-beginning-of-an-existing-array/35220/1 "2024-01-12T12:26:47Z")

</div>

Hi,

_Please note that the **nodes** I’m talking about below are not n8n nodes.  
They are arrays in the JSON data I’m manipulating._

My goal is to add a Table of Contents (ToC) at the _beginning_ of a very long draft blog post on the Wix platform to improve navigation within the blog post (jump to specific sections).

My technical challenge: merging the initial draft blog post’s content with the ToC content I’ve generated (JSON).

So far I’ve successfully retrieved the post’s as JSON from their REST API, extracted the richContent property, filtered it to keep only HTML header data (H2, H3, …) and transformed these headers to a new JSON that contains the ToC.

The richContent property contains an array of “nodes” used to describe the content: paragraphs of text, images, links, headers, …

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/f/7/f74a18979ff8a08fe698ba7e1700797cbbb4bb01.png)

I’ve generated the ToC as a JSON that contains an array of “nodes” using the same format.

## Describe the problem/error/question

Now, I just need to insert the ToC “nodes” at the beginning of the initial richContent “nodes” in order to update the draft post’s content so I can finally update it on the Wix server.

Say for example that my richContent initial “nodes” array contains 150 nodes. I extract 7 headers from it and generate 7 ToC nodes. I now need to add these 7 to the richContent.nodes property that will contain 157 items as a result (the first 7 being the ToC nodes).

I’ve made attempts with the Code block and the Item Lists block (Operation: Concatenate) but couldn’t figure it out.

At the end of the process, I need the draft blog post JSON object (retrieved at the beginning of the workflow) to be updated so that its richContent.nodes property contains the ToC nodes + all the initial nodes. So I can finally call the Wix REST API’s UpdateDraftPost endpoint and include the updated draftPost object.

## What is the error message (if any)?

None.

## Please share your workflow

## Share the output returned by the last node

## Information on your n8n setup

- **n8n version:** 1.17.1
- **Database (default: SQLite):** default
- **n8n EXECUTIONS\_PROCESS setting (default: own, main):** default
- **Running n8n via (Docker, npm, n8n cloud, desktop app):** n8n cloud
- **Operating system:** Windows 11

---

<div class="post-metadata">

**Author:** ![Jon](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jon/32/40174_2.png) [@Jon](https://community.n8n.io/u/Jon)\
**Post date:** [January 12, 2024, 3:26pm UTC](https://community.n8n.io/t/merging-json-inserting-array-items-at-the-beginning-of-an-existing-array/35220/2 "2024-01-12T15:26:47Z")

</div>

Hey @tomtom,

Would you be able to provide some example json so we can test your current workflow example and see what happens? It all depends on if the ToC data is also in the richContent data already somewhere or if is in another node as well but I suspect the easiest option would be to use a code node and write a bit of javascript to merge the arrays.

If you can get us some sample data we can take more of a look for you.

---

<div class="post-metadata">

**Author:** ![tomtom](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tomtom](https://community.n8n.io/u/tomtom)\
**Post date:** [January 15, 2024, 10:43am UTC](https://community.n8n.io/t/merging-json-inserting-array-items-at-the-beginning-of-an-existing-array/35220/3 "2024-01-15T10:43:46Z")

</div>

Thanks Jon.

I created this workflow **to generate** the ToC data nodes as JSON in the expected format and then to insert it inside the richContent property of the draftPost object received from the HTTP request GetDraftPost so I can finally update the draft post on the Wix server.

To answer your question: the ToC data is not in the richContent already. It doesn’t exist initially, it’s generated by my workflow.

In the draft post retrieved at the beginning of the workflow, the richContent property contains an array of nodes. Each node describes a content item for the post. Paragraphs are nodes, HTML headers are nodes (H2, H3, H4…), images are nodes.

For example: the draft post contains a paragraph of text, then a H2, then another paragraph. That’s 3 nodes in the richContent property. This workflow extract all nodes where node.type = “HEADING” and creates additional nodes based on them (internal links to these headings).  
So in this example with 3 nodes, including 1 heading, I want to create 1 additional node based on the HEADING data and insert it as the first node so that I have 4 nodes in the end.

Example: initial draftPost

> My first paragraph (this is 1 node)
> 
> ## My H2 heading (this is 1 node)
> Another paragraph (this is 1 node)

Expected draftPost after workflow:

> _My H2 heading_ (this is 1 new ToC node, an internal link to the actual H2 node)
> 
> My first paragraph (this is 1 node)
> 
> ## My H2 heading (this is 1 node)
> Another paragraph (this is 1 node)

So the goal is to create new nodes (node.type = “ANCHOR”, meaning internal links within the post) and insert them as the first nodes of the richContent.

Complete export of an example draftPost:

> **[draftPost.json](https://drive.google.com/file/d/1M8GeIincF-zBdh2f2Tp7AiCnMZdByBIz/view?usp=drive_link)**
>
> Google Drive file.

That’s what I retrieve from Wix  
What matters: draftPost.richContent.nodes.  
That’s where you find the various types of content nodes.  
You can do a CTRL+F to find nodes where type = “HEADING”.  
That’s the ones I extract.

From here, I generate the ToC content, ie a list of nodes that contain internal links to the various headers.  
New ToC nodes generated (they are all ANCHOR types pointing to HEADING) :

> **[ToC - new nodes.json](https://drive.google.com/file/d/1ccYW2yvIQIzfKY2s5GdSOf9gT7joJiMv/view?usp=sharing)**
>
> Google Drive file.

Then, I need to insert these new ToC nodes as the first nodes of the draftPost.richContent.nodes array.  
That’s what I call merging.  
I merged the example manually in this one:

> **[merged](https://docs.google.com/document/d/1Cm39gcbgipF0t47rdA6vYZas2lKf5owAbdk1LJpxiDI/edit?usp=sharing)**
>
> This Doc is private

Finally, I’ll need to send the updated (merged) draftPost back to Wix but that’s a different story (HTTP PATCH request).

---

<div class="post-metadata">

**Author:** ![tomtom](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tomtom](https://community.n8n.io/u/tomtom)\
**Post date:** [March 2, 2024, 1:45pm UTC](https://community.n8n.io/t/merging-json-inserting-array-items-at-the-beginning-of-an-existing-array/35220/4 "2024-03-02T13:45:38Z")

</div>

Any help welcome

---

<div class="post-metadata">

**Author:** ![Jon](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jon/32/40174_2.png) [@Jon](https://community.n8n.io/u/Jon)\
**Post date:** [March 4, 2024, 4:53pm UTC](https://community.n8n.io/t/merging-json-inserting-array-items-at-the-beginning-of-an-existing-array/35220/5 "2024-03-04T16:53:40Z")

</div>

It is going to take me a minute to catch up on this one, Which node is draftpost.json coming out of and which node is ToC - new nodes.json coming from?

---

<div class="post-metadata">

**Author:** ![tomtom](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tomtom](https://community.n8n.io/u/tomtom)\
**Post date:** [March 5, 2024, 10:02am UTC](https://community.n8n.io/t/merging-json-inserting-array-items-at-the-beginning-of-an-existing-array/35220/6 "2024-03-05T10:02:08Z")

</div>

Extract Rich Content generates draftpost.json

Create ToC node generates ToC - new nodes.json

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/195824_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [June 3, 2024, 10:02am UTC](https://community.n8n.io/t/merging-json-inserting-array-items-at-the-beginning-of-an-existing-array/35220/7 "2024-06-03T10:02:19Z")

</div>

Closed on June 3, 2024, 10:02am UTC

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