Goal: merge two json objects where one of them is found in a basic text column
Merging two json objects created as a json object in glide is super simple by using JSON template column with $merge([$json1, $json2])
But if one of the variables is a basic column which has valid json then it isn’t possible
I have tried different approaches
One is with JavaScript
function mergeJsonObjects(jsonObject, jsonString) {
// Parse JSON-teksten til et JavaScript-objekt
const jsonObjectFromString = JSON.parse(jsonString);
// Merge de to objekter ved hjælp af spread syntax
const mergedObject = { ...jsonObject, ...jsonObjectFromString };
return mergedObject;
}
return mergedObject = p2,p1)
This code will not run and I just get a spinning wheel
I have also tried to create a new JSON object with glides json object column. That won’t work either as the basic column is interpreted as text and “ is escaped \”
It should look like this, assuming your function is correct:
function mergeJsonObjects(jsonString, jsonObject) {
// Parse JSON-teksten til et JavaScript-objekt
const jsonObjectFromString = JSON.parse(jsonString);
// Merge de to objekter ved hjælp af spread syntax
const mergedObject = { ...jsonObject, ...jsonObjectFromString };
return mergedObject;
}
return mergeJsonObjects(p1, p2)
@Darren_Murphy well it’s the same problem. Spinning wheel
function mergeJsonObjects(jsonString, jsonObject) {
// Parse JSON-text to a JavaScript-object
const jsonObjectFromString = JSON.parse(jsonString);
// Parse JSON-object to a JavaScript-object
const jsonObjectFromObject = JSON.parse(jsonObject);
// Merge de to objekter ved hjælp af spread syntax
const mergedObject = { ...jsonObjectFromObject, ...jsonObjectFromString };
return mergedObject;
}
return mergeJsonObjects(p1, p2)
I’m not sure without trying it, but usually when debugging javascript, I’ll put in random returns in the code that return dummy text, to see how far the code gets before it fails. Sometimes I’ll I pect the console in the browser to see what errors I get. Other times I’ll add a try…catch to the code and have it return any exception errors. Something like this.
function mergeJsonObjects(jsonString, jsonObject) {
try {
// Parse JSON-text to a JavaScript-object
const jsonObjectFromString = JSON.parse(jsonString);
// Parse JSON-object to a JavaScript-object
const jsonObjectFromObject = JSON.parse(jsonObject);
// Merge the two objects using spread syntax
const mergedObject = { ...jsonObjectFromObject, ...jsonObjectFromString };
return mergedObject;
} catch (error) {
return error.toString();
}
}
return mergeJsonObjects(p1, p2);
Something like that might help reveal what’s happening in the code.
Are you passing actual JSON objects into the javascript, or just text? Within Glide, it seems like the javascript column can only reliably accept and return values as text.
There was a formatting error in the results copied in this thread due to incorrect quotation marks. If you use the following script, it will also work correctly:
@Himaladin@ThinhDinh both solutions work great. Which solution is best just depends on which output you want. I prefer the pretty printed version.
You can use Query JSON column on both