Create json from whole table

I would like to create a json output based on all the information in a table (number of rows can vary)

image

{
“Which wine grapes do you prefer”:“Cabernet Savignon,Malbec”,
“Which wine countries do you prefer”:“Argentina, France”,
“Can you name a wine that you like”:“El Enemigo Cabernet Franc”,
“Do you like older or younger wines”:“Older”,
“What is the best wine you have tasted”:“Mouton Rotschild”,
“Which year are you born”:“1968”
}

Why,
I want to store the information that I used for a specific openai table question query in one single field (in another table along with the prompt and answer sendt to the openai table question query)

Wonder how that could be done?

Template → Joined List → Template should do that.

The first template to combine each Q&A as JSON,
The Joined List to combine all lines,
And the second template to add the outer curly braces.

2 Likes

Good example to understand and practice what we can receive if the Make JSON Object plugin is used.

At first glance, anyone would be tempted to use it (like me) and the result would be:

and the Joined List will be:

Unfortunately, this structure lacks outer curly braces so, we need to create another Template column to add them to and get this:

[
 {"Which wine grapes do you prefer":"Cabernet Savignon,Malbec"}, 
 {"Which wine countries do you prefer":"Argentina, France"}, 
 {"Can you name a wine that you like":"El Enemigo Cabernet Franc"}, 
 {"Do you like older or younger wines":"Older"}, 
 {"What is the best wine you have tasted":"Mouton Rotschild"}, 
 {"Which year are you born":1968}
]

By accident we have created an objects array with 6 items … WTF! :woozy_face:

This forces you to type the array’s index and the question at the same time in the JQ Query field to get the correct answer. Too much effort!!

Instead, if we carry out the Darren’s suggestion the life will be nicer :beers:

We would just have a Joint List like this (not an array):

{
  "Which wine grapes do you prefer":"Cabernet Savignon,Malbec", 
  "Which wine countries do you prefer":"Argentina, France", 
  "Can you name a wine that you like":"El Enemigo Cabernet Franc", 
  "Do you like older or younger wines":"Older", 
  "What is the best wine you have tasted":"Mouton Rotschild", 
  "Which year are you born":1968
}

and finding results is easier, we just write the question and that’s it:

Thanks for the question and answer @Krivo and @Darren_Murphy!

Feliz fin de semana!

3 Likes

hehe yes, my initial thought was the MakeJSON column. But as you point out, that actually creates extra work :slight_smile:

There is one problem with my solution, and that is that it could break if any of the answers contained characters that need to be escaped in JSON. eg. double-quotes, line breaks, etc.

So probably a more robust solution would be to create two joined lists using an obscure separator (eg. a double pipe), and then use those to build a JSON object with JavaScript. Something like:

const keys = p1.split("||");
const values = p2.split("||");

const result = {};

for (let i = 0; i < keys.length; i++) {
  result[keys[i]] = values[i];
}

return JSON.stringify(result);

Using the above, if one of the answers contained double-quotes (for example), we would still get valid JSON.

3 Likes

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