Angular 8 http.get bind a certain property only

zxc

Is there a way for angular http.get to deserialize a json object without the need of the root object?

json:

{
        "jsonprop" : [
            {
                "id" : 1,
                "name" : "testName"
            },
            {
                "id" : 2,
                "name" : "testName2"
            }
        ]
}

models:

export interface Jsonprop {
    id: number;
    name: string;
}

export interface RootObject {
    jsonprop: Jsonprop[];
}

http method: (working but this is not what i wanted)

getJsonProp(searchText: string): Observable<RootObject> {
    return this.http.get<RootObject>('apiurl');            
}

What I wanted to do instead is :

getJsonProp(searchText: string): Observable<Jsonprop[]> {
    return this.http.get<Jsonprop[]>('apiurl');            
}

Is there a way for me to do this?

Nicholas K

You can use the map() operator from RxJS. For eg:

return this.http.get<RootObject>('apiurl').pipe(
   map(response) => {
      return response.jsonprop;
   })
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular 7 rxjs 6 bind to certain elements only

Bind to property only if it exists

Bind different values in multiple textbox with same ngModel property in Angular 8

Access HTTP GET JSON property in angular controller

Angular How to only intercept Http calls made by certain service

Get only 8 records from algolia in angular

Angular 8 : this.http.get is not a function

http.get not executed in Angular 8

this.http.get is not a function angular8

Angular Emit event only if method does not get called for a certain timespan

TypeError: Cannot read property 'message' of null angular 8 http post

Angular 2 - Cannot read property 'get' of undefined (http undefined)

Can't bind to 'formGroup' since it isn't a known property of 'form' in Angular 8

EventEmitter - Can't bind since it isn't a known property - Angular 8

bind image dynamically Angular 8

Angular: $http.get() only fires every second onpopstate trigger

Typescript Angular HTTP request get only last result

HierarchicalDataTemplate Bind only data that has a property

Angular 8 Http Post get boolean value from subscribe

IE8 changing GET request to POST: Angular $http

How to get http POST's response's headers in Angular 8

How to send firebase jwt getIdToken to http get request in angular 8?

how to change null to empty value in http get request angular 8

Get the Status Text from a http Service - Angular 8

JavaFX 8 - Bind TableView Selection Model Property

How to get selected object from valueChanges but only bind object id to form using Angular reactive forms?

Bind combobox items source to certain property of those objects

Conditionally bind a property in Angular2/4

ngModel cant bind to async property Angular