Javascript - are some functionality not available

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.

image

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;

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; 

2 Likes

@Darren_Murphy Doesn’t seem to work :frowning:
I have checked that the data in p1 is actually valid json (Best JSON Viewer and JSON Beautifier Online)

Any hints?

image

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;

See my previous reply :point_up:

@Darren_Murphy I keep repeting myself. You are a star :star: :star: :star: :star: :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?

1 Like

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.
1 Like

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);
1 Like

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