0

Below is my code to get response from webapi i am able to get results as count is showing right but it coming in [object object] format how to get proper json below is my line of code

enter image description here

const userList: ISuggestionItem[]=[];
      this.props.context.aadHttpClientFactory
      .getClient('xxxxxx-xxx-xxx-xxx-xxxx')
      .then((client: AadHttpClient): void => {
        client
          .get('https://xxxxx.azurewebsites.net/api/xxxxxx?userEmail=xxxxx.onmicrosoft.com', AadHttpClient.configurations.v1)
          .then((response:HttpClientResponse)=> {  
            
            console.log("First hit" + response)
                     response.json().then((responseJSON: any) => {
                      console.log("Second hit" + responseJSON.stringify)
                       responseJSON.map(function(item:any) {
                        console.log("third hit" + item.stringify)
                           userList.push({
                                "key" : item.Test1,
                               "displayValue"  : item.Test2,
                               "searchValue": item.Test3
            
                           });
                           
            
                        }) 
      });
     
    });  
        }); 
Ashok
  • 59
  • 1
  • 10

2 Answers2

1

[object Object] is the default stringification method of a JS object. You can display a JSON string with JSON.stringify(theObject).

amycodes
  • 902
  • 14
1

The response JSON is an object.

If you need to print the JSON object in console, you need to stringify the object.

Try the below code.

response=JSON.stringify(response);
console.log("First hit" + response);

This will work in your case. For more detail refer here

kelsny
  • 23,009
  • 3
  • 19
  • 48
Pranav MS
  • 2,235
  • 2
  • 23
  • 50