0

I have a service that gets the result in an API. API returns the results as JSON array. But I need the result as JSON object.

API returns:

[{"record_id":"101","demographics_spring_complete":"2"}]

I need as object format:

{"record_id":"101","demographics_spring_complete":"2"}

My service.ts:

getStatus() {
    return this.http.post(this.baseUrl).map(res => res.json())
        .catch(this.handleError);
}

And my conponent.ts:

export class BaselineListComponent implements OnInit {  
statuses: any[] = []; 

constructor(
   public recordService: RecordsService
 ) { }

 loadStatus() {
    this.recordService.getStatus('demographic_spring', 'baseline_visit_a_arm_1', 101).subscribe(statuses => {
  this.statuses = statuses;
  console.log(this.statuses);
   });
  }
}

I did try some conversion options but since API returns an array, I am unable to convert them to a JSON object. Any help would be appreciated.

3 Answers 3

1

It seems you need the first Object of the array, use array[index] as follows,

loadStatus() {
    this.recordService.getStatus('demographic_spring', 'baseline_visit_a_arm_1', 101).subscribe(statuses => {
     this.statuses = statuses;
     console.log(this.statuses[0]);
   });
}
Sign up to request clarification or add additional context in comments.

Comments

1

Add a do operator and return the first element.

getStatus() {
    return this.http.post(this.baseUrl).map(res => res.json())
        .do(res => res[0])
        .catch(this.handleError);
}

1 Comment

That still gives me as an array.
0

Create model class (record.model.js)

export class Record {
   constructor(
    record_id: string,
    demographics_spring_complete : string,
   ){}
}

At Service.js Class You need to code like this:

Import some Library at Service.js class

import { Record } from '../models/record.model'
import 'rxjs'
import { Observable } from 'rxjs/Rx'

create the functions to get the record

   getRecords(): Observable<Record[]> {
    return this.http.get(this.userApiUrl)
            .map((res: Response) => res.json())
            .catch((error: any) => Observable.throw(error.json().error || 'Server Error'))
  }

In the component.ts file Add global variable:

record: Record;

then subcribe the record function of service

getRecord(): void {
 this.recordService.getRecords().subscribe((result: any) => {
    this.record = result && result[0] || {};
 }, (error: any) => {
   console.log(error);
 });
}

if the result return [{"record_id":"101","demographics_spring_complete":"2"}], result[0] will return {"record_id":"101","demographics_spring_complete":"2"}, but if the result has not value undefiend execption will be occured, we will use this.record = result && result[0] || {}; to handled the execption.

Comments

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.