JSON Transformer - merging two JSON structures into one

At a certain point in my workflow, I have to JSONs to handle. Let’s assume their contents as follows. Please note, both the structure and the content is completely made up and they have nothing in common with the data I work in my application with.

JSON A:

{
	"configuration" : {
		"local" : {
			"host" : "localhost",
			"port" : 1313
		},
		"remote" : {
			"host" : "192.168.7.8",
			"port" : 1313
		},
		"modules" : [
			{"name" : "integration",
			"secured" : "true"},
			{"name" : "math",
			"cpu_consumption" : "high"}
		]
		
	}
}

JSON B:

   {
	"initial_data" : {
		"musicians" :
		[ 
			{"name" : "roger",
			"groups" : ["floyd", "bass guitar players", "guitar players"]},
			{"name" : "david",
			"groups" : ["floyd", "guitar players"]}
		],
		"genres" : [
			{"name" : "rock"},
			{"name" : "jazz"}
		]
		
	}
}

We have two JSON structures consisting of a non-flat structure beneath a single root node. I do not know the particular structure of the JSONs at design time.

Now, my aim is to combine these two structures into a single JSON. I don’t care whether the logic I’m going to end up with will produce an additional root node with two nodes underneath or there will be a pair of root nodes there. So both resulting structures are OK for me:

Option X

{“root” : {
    “configuration” : {...},
    “initial_data” : {...}
}}

Option Y

{
    “configuration” : {...},
    “initial_data” : {...}
}

Although the task seemed simple to me, the silly thing is I couldn’t figure out how to achieve the result unless I performed such a weird thing like transforming the JSONs to Strings, concatenate them and parse back to JSON, which I’d consider pretty ugly.

Well, I tried introducing NodePit JSON Transformer node.

let’s have a look at a test workflow:

First I proved the assumption the node is capable of performing such manipulation if the JSON structure to merge with is hard-coded in the Expression text area.

We have JSON A in json_a column and we merge it with text content of json_b document.

The result is exactly as expected.

Now, if I replace the hard-coded part with a reference to a column containing corresponding content, it stops working. Here is an updated configuration of the JSON Transformer node:

I replaced Expression with a reference to json_b column. Unfortunately, the result is:

Then I tried moving json_b column to a flow variable but this attempt failed as well.

Here we can see what the json_b flow variable contains:

The error message is a bit misleading here but I don’t care

To keep this post reasonably concise, after a number of subsequent attempts of many sorts I didn’t manage it.

I’d appreciate any advice here. Thanks.

For now, there is a #workaround.


I added two new nodes to the workflow. The first one joins both JSON as strings. Also, it replaces curly brackets at the seam with comma. The second node transforms the concatenated string back to JSON.

Here we go:

The solution is ugly and it tends to fail if there occurs the sequence I used for marking the place the second JSON follows the first one, somewhere else in one of the JSON. This risk is acceptable for me in this case so I ignore it.

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