I am using the model adapter pattern in angular application. How to map this nested json api response to a model in angular?`
[
{
"2023": {
"stock_market": {
"January": {
"invested": "337",
"earned": "332.8"
},
"February": {
"invested": "306",
"earned": "299.4"
},
"March": {
"invested": "334",
"earned": "332.0"
},
"April": {
"invested": "324",
"earned": "319.7"
}
}
}
},
{
"2024": {
"stock_market": {
"January": {
"invested": "337",
"earned": "332.8"
},
"February": {
"invested": "306",
"earned": "299.4"
},
"March": {
"invested": "334",
"earned": "332.0"
},
"April": {
"invested": "324",
"earned": "319.7"
}
}
}
}
]
I have tried to flattened the object. But not fully understanding how to approach this. also how to modify this above json and use adapter with this below format in angular?
{
totalInvested: '2334',
returnPercentage: '4.4%',
description: 'from last year',
interpretation: 'good returns',
title: 'Total profit or loss',
}
you can create TypeScript Interface to represent the data model and then map the nested API JSON response to instances of those classes
Next, in your Service, import the interfaces and handle the API response:
finally use the service in your component to fetch the data and map it to the method
You can follow this example aswell Try it out
I hope it will help you thank you :D