Help to understand the Different outputs from JSON Template column vs. normal template column

Hi,

I would like to understand the difference in output between the Template column and the JSON Template column in this specific case. I am trying to create a API payload for the Call API like this:

1. Using a JSON Object column to create columnValues. The column outputs this:

{
  "1RRR0": "rRoW8v6HT4qJoTSb1fIDcw",
  "Usz92": "d11e140a-877a-41b0-b530-3769d17ab3cb"
}

2. Using a JavaScript column to create mutations. The column outputs this:

[{"kind": "add-row-to-table", "tableName": "native-table-OixoB8WxaCD1brEuIgw2", "columnValues": {"1RRR0":"IAj1kNiHSbG1dUa3Xzy3yg","Usz92":"ff90fdcd-11a4-4ca2-8084-8e5baa1b7da3"}}]

3. When using a JSON Template columnd to create the payload it looks like it is converting the array into a string, because the output looks like this:

{
  "appID": "Xn0neojnKCfEmqgybd6D",
  "mutations": "[{\"kind\": \"add-row-to-table\", \"tableName\": \"native-table-OixoB8WxaCD1brEuIgw2\", \"columnValues\": {\"1RRR0\":\"IAj1kNiHSbG1dUa3Xzy3yg\",\"Usz92\":\"ff90fdcd-11a4-4ca2-8084-8e5baa1b7da3\"}}]"
}

However, when I use a normal template column with the same settings, I get the output that I need to create the payload:

{
  "appID": "Xn0neojnKCfEmqgybd6D",
  "mutations": [{"kind": "add-row-to-table", "tableName": "native-table-OixoB8WxaCD1brEuIgw2", "columnValues": {"1RRR0":"IAj1kNiHSbG1dUa3Xzy3yg","Usz92":"ff90fdcd-11a4-4ca2-8084-8e5baa1b7da3"}}]
}

Can someone please explain why this is happening? I am trying my best to learn JSON and use it more, but I am really struggling to understand it :flushed:

The problem is that the JavaScript column is not “JSON aware”, and so it returns a stringified version of the JSON rather than a JSON object.

It’s best to avoid using JavaScript to generate JSON where ever possible, and stick to the native JSON columns that Glide provides. I know this isn’t always possible, but you just need to be aware of the difference.

Thank you! I understand. So in this case, what would be the preferred way to handle it? Since I need to use the JavaScript column in this case, should I just keep using the normal Template Column? Or is there a better way to approach this. Just want to make sure that I follow good practices here.

My rule of thumb for generating JSON is that I will always use the native JSON columns where I can, and will only turn to JavaScript as a last resort.

1 Like

Understood. My use case here is what was discussed in the thread below and I chose to go with the JavaScript approach so I guess in this case I will need to use the Template column.

ah, right. Yeah I think that is an example of a use case where you might need to use JavaScript.

1 Like

You still use this, then in the step just before using JSON Template, you:

  • Create a JSON Query column, call it something like “JSON Formatted”.
  • Target the result of the JavaScript column.
  • Don’t write any queries, it should be kept empty.
  • Use the result of JSON Query in your final JSON Object column.
2 Likes

ah, nice. That never occurred to me

1 Like

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