2

The json response I get from an API is in this format:

{
    "bill_id": 110,
    "billdate": "2021-06-23",
    "amount": 5500,
     "vendors": {
        "vendor_id": 7,
        "vendor_name": "ABC pvt ltd",
    }
}

So I am creating an interface to map with response.

export interface BillModel{
    bill_id:number,
    billdate:string,
    amount: number,
    vendors:object
}

I don't know how to make the vendors field and object with an interface too. [vendor_id and vendor_name]

That way I can map the http response properly

 return this.http.get<BillModel[]>(this.getbillurl);

I know it is very basic but I am in learning the phase. Please suggest.

Brian Smith
  • 1,467
  • 15
  • 31
Ajay J
  • 49
  • 8

1 Answers1

3
export interface Vendor {
    vendor_id: number,
    vendor_name: string
}

export interface BillModel{
    bill_id:number,
    billdate:string,
    amount: number,
    vendors: Vendor
}

Although by the name it sounds like you might want vendors: Vendor[] and be expecting an array?

Alex
  • 848
  • 6
  • 7
  • Thanks Now I understood – Ajay J Jun 25 '21 at 14:42
  • it is not possible with class? why do you choose interface? – Kumaresan Perumal Dec 06 '22 at 11:44
  • @KumaresanPerumal You can do it with a class if you like. I use interfaces for http responses because they don't actually end up in your compiled code, and I'm not usually doing type checking at runtime for http responses. See https://stackoverflow.com/questions/51716808/when-use-a-interface-or-class-in-typescript – Alex Dec 06 '22 at 17:13