how to access caption from Semantic Search using JavaScript?

Kenneth Cheung 20 Reputation points
2023-12-31T14:41:20.3366667+00:00

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.

caption

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

Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
1,225 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
1,051 questions
Azure Static Web Apps
Azure Static Web Apps
An Azure service that provides streamlined full-stack web app development.
1,103 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Kenneth Cheung 20 Reputation points
    2024-01-02T00:35:20.28+00:00

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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.