I have an json result which has nested objects. I need to cast them to my custom objects ( prime ng tree table json) which have different property names than the json result. >
I'm assuming that the 'json result' comes from backend call. So the best and easiest way would be to map the observable to your destination format.
You should use HttpClient (the call to backend will return you an Observable) -> https://angular.io/guide/http
Create an interface, that will describe json object field names (let's call it PersonalDetails):
export interface PersonalDetails {
brinname: string,
aantalPersonen: string,
...
}
There can be several interfaces (the json object is really big, so it might be good to split it and create other interfaces, that will be nested inside the first one).
Create interface for 'prime ng tree table json' => same as above, let's call it PrimeNgTableData
Create a function, that will take a parameter of type 'PersonalDetails' (from point 2.) and will cast it to second interface PrimeNgTableData (from point 3.). Let's name that function like that:
export function asPrimeNgTableData(personalDetails: PersonalDetails): PrimeNgTableData {
return {
...
}
}
private personalDetails$: Observable = this.someServiceThatCallsHttp.getPersonalDetails();
private primeNgTableData$: Observable = this.personalDetails$.pipe(
map((personalDetails: PersonalDetails) => asPrimeNgTableData(personalDetails))
);
....