Krivo
December 28, 2023, 3:39pm
1
I’m total javascript novice - so bear with me
I tried to convert from json to markdown (see post below)
I use the below code - but it seems as it wont run due to unknown forEach. When I have tried to use javascript in glide earlier on then I often cannot get the javascript to run.
Anyway to fix this?
let markdownTable = “| Store | Title | Period |\n|-------|-------|--------|\n”;
p1.forEach(item => {
let period = item.dateRange.replace("Present", "Now");
markdownTable += `| ${item.storeName} | ${item.jobShort} | ${period} |\n`;
});
return markdownTable;
Two ways I can think of to approach this:
Use a JavaScript column to parse the JSON and output the HTML table. The dates would be a bit fiddly, but other than that it should be pretty straight forward. (ChatGPT would be able to help you).
Use a series of Query JSON columns inside a Helper Table to pull the individual values, and then build the HTML with a Template and Joined List. Again, the dates would be a bit fiddly.
Now that I’ve typed that out, I think I’d favour the first option
Is that the whole code?
Assuming that p1 is your JSON string, then you need to parse it into an object first.
Change your code as follows:
const json = JSON.parse(p1);
let markdownTable = “| Store | Title | Period |\n|-------|-------|--------|\n”;
json.forEach(item => {
let period = item.dateRange.replace("Present", "Now");
markdownTable += `| ${item.storeName} | ${item.jobShort} | ${period} |\n`;
});
return markdownTable;
@Krivo - I just noticed that there are “smart” quotes on line 2 (because I copied/pasted what you gave), which breaks the code.
Here is a working and tested version:
const json = JSON.parse(p1);
let markdownTable = `| Store | Title | Period |\n|-------|-------|--------|\n`;
json.forEach(item => {
let period = item.dateRange.replace("Present", "Now");
markdownTable += `| ${item.storeName} | ${item.jobShort} | ${period} |\n`;
});
return markdownTable;
Krivo
December 28, 2023, 4:01pm
4
@Darren_Murphy Doesn’t seem to work
I have checked that the data in p1 is actually valid json (Best JSON Viewer and JSON Beautifier Online )
Any hints?
The code I’m using (change to " instead of “)
const json = JSON.parse(p1);
let markdownTable = "| Store | Title | Period |\n|-------|-------|--------|\n";
json.forEach(item => {
let period = item.dateRange.replace("Present", "Now");
markdownTable += `| ${item.storeName} | ${item.jobShort} | ${period} |\n`;
});
return markdownTable;
return markdownTable;
Krivo
December 28, 2023, 4:16pm
6
@Darren_Murphy I keep repeting myself. You are a star
As side node.
I used a “Query Json” as input to the javascript column - and even if looks as a valid JSON it is not.
I therefore used the “Query Json” column output as input in a template column - doing no manipulation with the input. The result was then valid JSON which the javascript accepted.
Should this be considered a bug?
I don’t know this for a fact, but my guess would be as follows:
the JavaScript column only accepts text as input
your Query JSON column is returning a JSON object (not a JSON string), therefore it fails when you pass it to the JavaScript column
passing it through a Template column converts the object to text.
Check for any “keys” that might contain double quotes, the column will render your JSON but it doesn’t handle this error. You can run it through something like https://jsonlint.com/ .
Always make sure you parse the JSON because it’s seen as text in the input phase like Darren wrote out.
const json = JSON.parse(p1);