JSON parse not working in 0codekit

const queryEmbedding = [{{4.data.embedding}}];
const rows ={{40.json}}; // Array of row objects from Google Sheets

function cosineSimilarity(a, b) {
let dot = 0.0, normA = 0.0, normB = 0.0;
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i];
normA += a[i] * a[i];
normB += b[i] * b[i];
}
return dot / (Math.sqrt(normA) * Math.sqrt(normB));
}

const matches = ;

for (const row of rows) {
try {
// Try to get embedding column (adjust if column name is different)

const rawEmbedding = row.Embedding;

const embedding = rawEmbedding;

const similarity = cosineSimilarity(queryEmbedding, embedding);

matches.push({
  Question: row.Question,
  Answer: row.Answer,
  Category: row.Category,
  Similarity: similarity
});

} catch (err) {
result = {“matches”: err};
}
}

matches.sort((a, b) => b.Similarity - a.Similarity);
const topK = matches.slice(0, 3);
const formattedMatches = topK.map(m =>
Q: ${m.Question}\nA: ${m.Answer}
).join(“\n\n”);

result= { matches: topK };

Why is it not able to parse the JSON even if I write const embedding = JSON.parse(rawEmbedding);

Hello Chhaya,

I am part of the 0codekit dev team. This issue might just be related to how JavaScript normally works. For easier debugging, it’s recommended to also add temporarily the variables in question into the output JSON (so in this case, push it to matches) then you can download or copy and paste the output into ChatGPT to ask it for guidance on your JavaScript code. If you have any other issues, you can also directly reach out to https://www.0codekit.com/support

Best regards

0CodeKit Support

3 Likes