Currently, I am facing an issue regarding mapping a list of JSON data to my model. One of the properties in my model is defined as an enum type, but in the JSON data, that property is provided as a string. How can I correctly map this string to an enum value?
This is my enum definition:
export enum Status {
HIGH = "High",
MEDIUM = "Medium",
LOW = "Low"
}
Here is my model structure:
import { Status } from "../enums/status.enum";
export class OrderModel {
id: number;
notification: string;
action: string;
status: Status;
}
This is a sample of the JSON data I have:
[
{
"id": 1,
"notification": "Order has been packed",
"action": "Assign to delivery",
"status": "High"
}
]
When attempting to map the JSON data to my model, I encounter the following error (Type 'string' is not assignable to type 'Status'):
import { OrderModel } from '../../models/order.model';
import orderData from '../json/order.json';
@Injectable({
providedIn: 'root'
})
export class OrderService{
//Code for mapping JSON data to my model goes here
orderModel: OrderModel[] = orderData;
constructor() {}
getOrderStatus() {
console.log(orderModel)
}
}