0

I'm having an API request which returns the following:

{
    "FarmerDetails": [
   {
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
  },
  {
    "userId": 1,
    "id": 2,
    "title": "quis ut nam facilis et officia qui",
    "completed": false
  }
]
}

I need to show the title when I click the button and when the button clicks this "fetchPeople()" function gets call.

fetchPeople() {
    this.peopleService.fetchPeople().subscribe((res:any)=>{
      this.people$ = res;
    });
  }

How can I convert my Object to Array in typescript and show particular or all values.

3
  • This might help for you stackoverflow.com/questions/39308423/… Commented Feb 28, 2020 at 6:48
  • @Mitesh jadav, yes I tried that too and am using angular 8 Commented Feb 28, 2020 at 6:49
  • Did you try console.log(res.FarmerDetails);? Commented Feb 28, 2020 at 6:50

2 Answers 2

0

Handling an API response object is no different to handling any other JSON object. Just query it in the same way.

Given a response like this:

{
  "FarmerDetails": [
    // array of objects you want to extract from the response
  ]
}

You can try several approaches.

  1. Query the JSON object directly
fetchPeople() {
  this.peopleService.fetchPeople().subscribe((res:any)=>{
    this.people = res.FarmerDetails;
  });
}
  1. Map in the pipe
fetchPeople() {
  this.peopleService.fetchPeople().pipe(
    .map(res => res.FarmerDetails)
  ).subscribe(people => {
    this.people = people;
  });
}

When the people service emits a value that looks like the JSON above, it applies the transform in the map function before returning it to the subscriber.

  1. Pluck in the pipe
fetchPeople() {
  this.peopleService.fetchPeople().pipe(
    .pluck('FarmerDetails')
  ).subscribe(people => {
    this.people = people;
  });
}

Similar to map, when the people service emits a value that looks like the JSON above, it returns the named property in the pluck function before returning it to the subscriber.

Approaches 2 and 3 would be preferable, as you can apply these in you service. Assuming that these come from an HTTP request, you can use the pipe after the get:

export class PeopleService {
  fetchPeople() {
    return this.http.get(url).pipe(
      pluck('FarmerDetails')
    );
  }
}

Also, the "$" suffix on variables is generally used to denote an observable. In your example, you're just storing the value that is emitted by the observable, so it would be confusing to name it people$. I have named it people for this reason.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for your valuable time @Kurt Hamilton, In postman am using POST request for the URL " 169.38.82.132:94/GetFarmerInfo" and in my service file am using the code headers = new HttpHeaders({ 'Authorization': 'bearer uRf0e8upueR4qTh....', 'InstanceName': 'ORISSA' , 'Content-Type': 'application/json'}); fetchPeople() { return this.http.post('http://169.38.82.132:94/GetFarmerInfo', { headers: this.headers }); }; so am trying to pull data from this API and the result as above, If I updated the code getting response as "POST 169.38.82.132:94/GetFarmerInfo 401 (Unauthorized)"
Can you please add all relevant code to your question. You asked about how to get a value from a response, but it sounds like you have an entirely different problem.
My stackblitz code url stackblitz.com/edit/angular-pg6wqe, in this if i pass my api "169.38.82.132:94/GetFarmerInfo" instead of "jsonplaceholder.typicode.com/todos" in people.service.ts it is not working. as in the sample api the json response is json array and my response is json object with array, so am trying to convert json object to array to get the response. hope this clear...
I have forked your example, and am returning an object in your structure using of instead of an http request for demo purposes. Here I demonstrate the use of map and pluck: stackblitz.com/edit/angular-mkuusq
I have updated my stackblitz to make a real http request, getting the data as you described it
|
0

When you get response from api, it will return in string format. So, you require to convert it to json format. Parse it to json:

this.people=JSON.parse(res);

3 Comments

Are you able to log this.people? Are you getting response?
In console am getting "POST 169.38.82.132:94/GetFarmerInfo 401 (Unauthorized).
Then, that is totally different to your question. It's not returning the response and how can you even convert json object to array? According to the error, you are unauthorized. May be, you require some credentials.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.