Complex JSON Serialization in Django
I have a complex JSON (trimmed)
[
{
"CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents": [
{
"decimals": "-6",
"unitRef": "usd",
"period": {
"instant": "2021-09-25"
},
"value": "35929000000"
},
{
"decimals": "-6",
"unitRef": "usd",
"period": {
"instant": "2020-09-26"
},
"value": "39789000000"
},
{
"decimals": "-6",
"unitRef": "usd",
"period": {
"instant": "2019-09-28"
},
"value": "50224000000"
},
{
"decimals": "-6",
"unitRef": "usd",
"period": {
"instant": "2022-09-24"
},
"value": "24977000000"
}
],
"NetIncomeLoss": [
{
"decimals": "-6",
"unitRef": "usd",
"period": {
"startDate": "2021-09-26",
"endDate": "2022-09-24"
},
"value": "99803000000"
},
{
"decimals": "-6",
"unitRef": "usd",
"period": {
"startDate": "2020-09-27",
"endDate": "2021-09-25"
},
"value": "94680000000"
},
{
"decimals": "-6",
"unitRef": "usd",
"period": {
"startDate": "2019-09-29",
"endDate": "2020-09-26"
},
"value": "57411000000"
},
{
"decimals": "-6",
"unitRef": "usd",
"period": {
"startDate": "2021-09-26",
"endDate": "2022-09-24"
},
"segment": {
"dimension": "us-gaap:StatementEquityComponentsAxis",
"value": "us-gaap:RetainedEarningsMember"
},
"value": "99803000000"
},
{
"decimals": "-6",
"unitRef": "usd",
"period": {
"startDate": "2020-09-27",
"endDate": "2021-09-25"
},
"segment": {
"dimension": "us-gaap:StatementEquityComponentsAxis",
"value": "us-gaap:RetainedEarningsMember"
},
"value": "94680000000"
},
{
"decimals": "-6",
"unitRef": "usd",
"period": {
"startDate": "2019-09-29",
"endDate": "2020-09-26"
},
"segment": {
"dimension": "us-gaap:StatementEquityComponentsAxis",
"value": "us-gaap:RetainedEarningsMember"
},
"value": "57411000000"
}
]
}
]
I have created the following class to use so that its easier to massage the data in
@dataclass
class Period:
instant: str
startDate: str
endDate: str
@staticmethod
def from_dict(obj: Any) -> "Period":
_instant = str(obj.get("instant"))
_startDate = str(obj.get("startDate"))
_endDate = str(obj.get("endDate"))
return Period(_instant, _startDate, _endDate)
@dataclass
class Segment:
dimension: str
value: str
@staticmethod
def from_dict(obj: Any) -> "Segment":
if (obj):
_dimension = str(obj.get("dimension"))
_value = str(obj.get("value"))
return Segment(_dimension, _value)
@dataclass
class ShareBasedCompensation:
decimals: str
unitRef: str
period: Period
value: str
@staticmethod
def from_dict(obj: Any) -> "ShareBasedCompensation":
_decimals = str(obj.get("decimals"))
_unitRef = str(obj.get("unitRef"))
_period = Period.from_dict(obj.get("period"))
_value = str(obj.get("value"))
return ShareBasedCompensation(_decimals, _unitRef, _period, _value)
@dataclass
class CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalent:
decimals: str
unitRef: str
period: Period
value: str
@staticmethod
def from_dict(obj: Any) -> "CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalent":
_decimals = str(obj.get("decimals"))
_unitRef = str(obj.get("unitRef"))
_period = Period.from_dict(obj.get("period"))
_value = str(obj.get("value"))
return CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalent(_decimals, _unitRef, _period, _value)
@dataclass
class NetIncomeLoss:
decimals: str
unitRef: str
period: Period
value: str
segment: Segment
@staticmethod
def from_dict(obj: Any) -> "NetIncomeLoss":
_decimals = str(obj.get("decimals"))
_unitRef = str(obj.get("unitRef"))
_period = Period.from_dict(obj.get("period"))
_value = str(obj.get("value"))
_segment = Segment.from_dict(obj.get("segment"))
return NetIncomeLoss(_decimals, _unitRef, _period, _value, _segment)
@dataclass
class Company:
CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents: List[
CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalent]
NetIncomeLoss: List[NetIncomeLoss]
@staticmethod
def from_dict(obj: Any) -> "Company":
_CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents = [CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalent.from_dict(
y) for y in obj.get("CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents")]
_NetIncomeLoss = [NetIncomeLoss.from_dict(
y) for y in obj.get("NetIncomeLoss")]
return Company(_CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents, _NetIncomeLoss)
And I have the following Model and Serializer
class CashFlow(models.Model):
Id = models.AutoField(primary_key=True)
CompanyId = models.ForeignKey(Company, on_delete=models.CASCADE)
PeriodInstant = models.DateTimeField()
PeriodStartDate = models.DateTimeField()
PeriodStartDateEndDate = models.DateTimeField()
Currency = models.TextField()
Decimals = models.IntegerField()
CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents = models.TextField(
null=True)
NetIncomeLoss = models.TextField(null=True)
class CashFlowSerializer(serializers.ModelSerializer):
class Meta:
model = CashFlow
fields = ('CompanyId',
'PeriodInstant',
'PeriodStartDate',
'PeriodStartDateEndDate',
'Currency',
'Decimals',
'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents',
'NetIncomeLoss')
I am looping over the lists but I am unable to understand how to store the data to the database.
def get_data(url, dataset_to_retrieve):
jsonstring = json.loads(jsonData)
companyData = Company.from_dict(jsonstring[0])
for item in companyData.CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents:
CashFlow.CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents = item.value
test1(item)
CashFlowSerializer
for item in companyData.NetIncomeLoss:
CashFlow.NetIncomeLoss = item.value
test1(item)
def test1(item):
CashFlow.CompanyId = 1
CashFlow.PeriodInstant = item.period.instant
CashFlow.PeriodStartDate = item.period.startDate
CashFlow.PeriodStartDateEndDate = item.period.endDate
CashFlow.Currency = item.unitRef
CashFlow.Decimals = item.decimals
I am very new to Django, any help / suggestions would help me a lot. Thanks