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.

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Now I understood
it is not possible with class? why do you choose interface?
@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 stackoverflow.com/questions/51716808/…

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.