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

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, …

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

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.

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:

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) :

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:

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

Any help welcome

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?

Extract Rich Content generates draftpost.json

Create ToC node generates ToC - new nodes.json

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