Binding api's JSON attribut with my angular 2 model class


I have model class with some attribute name (I create models for local storage), how can I bind these attributes with my api response?


            export class User {
              private _id:number;
              private server_id?:number;
              private account_id:number;
              private code:number;
              private teamApp?:Team;
              private teamServer?:Team;
              private filialeApp:Filiale;
              private filialeServer?:Filiale;
              private login:string;
              private rang:string;
              private civilite?:string;
              private prenom?:string;
              private nom?:string;
              private email?:string;
              private lang:string ='FR';
              private phone?:string;
              private comment?:string;
              private isActif:number;
              private synchronizationDate?: Date;
              private addDate:Date;
              private editDate:Date;
              private lastViewDate:Date;
                  private isToSync?:number;
              constructor(values: Object = {}) {
                Object.assign(this, values);

API response:

"u_inscription_date":"2016-06-19 00:00:00",
"u_inscription_date_gmt":"2016-06-19 00:00:00",
"u_last_access_date":"2016-09-13 12:09:44",

REST call:

    return this._http.get(this.url+'parameters',this.options).share()
      .map((res:Response) => new User(res.json()))
      .catch((error:any) => Observable.throw(error.json().error || 'Server error'));


this is horrible --> if you want to make this --> give your class an interface with allowed keys, type the api response (only for programming purpose).

And in your case you need some functions to transform your backend repsonses in the form you want it ;). No magic, which transforms ‘u_lang’ to ‘lang’.

You have to do it on your own… So you could pass all attributes you want to the constructor like you should do it.


Thank you @bengtler !
So if I understand, I must make an interface for each class, and allow data with this interface, like:

    export interface User {
  name: string;
  email?: string;

export class UserService {
  me: User;

  constructor() {

  setUser(user: User) { = user;


yep :).

and if you need default values for some keys you can transform the interface to a class


@bengtler I found a treasure to be sure to have a correct model from JSON files !