Merging two json objects - not so easy

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 \”

Any suggestions ?

Just looking quickly at your javascript I see two issues.

  • First, your last line is calling a function that doesn’t exist. You are calling mergedObject, but the function is called mergeJsonObjects
  • Second, you are missing a left parenthesis on the function call, also on the last line. You need that left parenthesis before p2.
3 Likes

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)
3 Likes

@Jeff_Hager @ThinhDinh thanks a lot for suggestion and correction of JavaScript.

It doesn’t work unfortunately. I just get a spinning wheel in the JavaScript column

image

Any suggestions how to achieve the goal - combining a json object from a json object glide column and a basic text glide column?

And, any idea why it isn’t possible to use the JSON template glide column? It will not accept a json from a basic text glide colum

Let’s take the example in your screenshot. What result would you expect when you merge those two columns?

@ThinhDinh the resultat should be like

{
“A”: “a”,
“B”: “b”,
“C”: “c”,
“lines”: {“A”: “a”, “B”: “b”, “C”: “c”}
}

Just like when you use JSON template column with $merge([$json1, $json2])

Thanks for investigating

I see that you are only using the JSON.parse method on one of the inputs. What happens if you use it on both before applying the merge?

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

const p1Object = JSON.parse(p1);
const p2String = JSON.parse(p2);

// Merge object
const combinedObject = { ...p1Object, ...p2String };

const combinedString = JSON.stringify(combinedObject, null, 2);

return combinedString;

3 Likes

function combineJson(jsonString1, jsonString2) {
  // Parse both JSON strings
  const obj1 = JSON.parse(jsonString1);
  const obj2 = JSON.parse(jsonString2);
  
  // Combine the objects
  const result = {
    ...obj1,
    ...obj2
  };
  
  return JSON.stringify(result);
}

return combineJson(p1,p2)

I think the problem was because you did not stringify the result.

3 Likes

@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

Perfect. You rock :metal::metal:

2 Likes

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