how to access caption from Semantic Search using Javascript?

41 views Asked by At

I'm trying to build a web app that can perform a semantic search using natural language and return the answer as a result. Using response.data.results, I am able to see the caption text in the console under semanticSearch->captions->text.

console image

However I can't figure out what is the syntax to access that info. I tried response.data.results.semanticSearch.captions[0].text but that is giving me an error "TypeError: Cannot read properties of undefined (reading 'captions')"

Any advice would be much appreciated.

Thanks! Kenneth

2

There are 2 answers

0
choz On BEST ANSWER

So, assuming that JSON on your screenshot coming from the log response.data.results, it turns out it that's an array. You just need to specify the indice like;

const result = response.data.results[0]; // or 1? whichever you need;
console.log(semanticSearch.captions[0].text)

log screenshot

0
Kenneth On

I was able to use stringify to turn the object to a string, then parsed it accordingly.

    var output = JSON.stringify(response.data.results);                      
    var pos = output.indexOf("text"); 
    var partOutput = output.slice(pos+7,pos+2000);
    var pos2 = partOutput.indexOf("\"highlights\"");             
    var finalOutput = partOutput.slice(0,pos2-2);              
    console.log(finalOutput);