Home > OS >  Cast json data to map
Cast json data to map

Time:10-20

I'm retrieving data from Firestore and casting it to a interface... and my data structure is like that:

{
  "workingDays":["Sunday","Monday","Tuesday","Wednesday","Thursday","Saturday"],"logo":"https://firebasestorage.googleapis.com/v0/b/jameel-a59bf.appspot.com/o/providers/17hELzdI9cf25ouMPQIe/708B4FEC-4E3E-43F6-8383-45164E09B851.png?alt=media&token=2adb93f7-d94c-4179-bd78-0b34652a9766",
  "workingHours":["9:00AM","10:00AM","11:00AM","12:00PM","1:00PM","2:00PM","1:00AM"],
  "name":"DTOX Car Service Dpulze Cyberjaya",
  "services":[
    {
      "id":"carCleaning",
      "price":
      {
        "Sedan":10, 
        "SUV":20, 
        "SUVXXL":30
      }
    }
  ],
  "address":"High St, Biggar ML12 6BJ, United Kingdom",
  "location":{"_latitude":55.62360289999999,"_longitude":-3.5256982}}

but the problem is I'm trying to cast the service price to Map<string, number> but I'm not getting it as a map as it's not casting properly I have tried a lot of ways but I'm not able to cast it to map. I know I can cast it by making private instance like that:

export interface Price {
        Sedan: number;
}

but the values in price may differentiate its not static so it's able to be epandable that's why I want to cast it to Map<string, number>

CodePudding user response:

You just need to add index signature for string keys with value type equal to number

export interface price {
  [key: string]: number
}
  • Related