Search code examples
javascriptsearchazure-web-app-servicesemantic-ui

how to access caption from Semantic Search using Javascript?


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


Solution

  • 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