Home > database >  how to handle complex json where key is not always present using python?
how to handle complex json where key is not always present using python?

Time:03-04

I need support in getting the value of Key issues which is not always present in JSON.

my JSON object is as below -

{
  "records":[
    {
    "previousAttempts": [],
    "id": "aaaaa-aaaaaa-aaaa-aaa",
    "parentId": null
    },
    {
    "previousAttempts": [],
    "id": "aaaaa-aaaaaa-aaaa-aaa",
    "parentId": null,
    "issues":[
      {
      "type": "warning",
      "category": "General"
      },
      {
      "type": "warning",
      "category": "General"
      }
    ]
    }
  ]
}

CodePudding user response:

You can use Pydantic to deserialize JSON string to Pydantic object. You can make a dict object from pydantic object then. You can set default value for fields you need.

You can use validators to validate loaded values.

from pydantic import BaseModel, Field


class AttemptModel(BaseModel):
    pass


class IssueModel(BaseModel):
    type: str
    category: str


class RecordModel(BaseModel):
    id: str
    parent_id: int = Field(alias='parentId', default=None)
    previous_attempts: list[AttemptModel] = Field(alias='previousAttempts')
    issues: list[IssueModel] = Field(default=[])


class DataModel(BaseModel):
    records: list[RecordModel]


data = """{
  "records":[
    {
        "previousAttempts": [],
        "id": "aaaaa-aaaaaa-aaaa-aaa",
        "parentId": null
    },
    {
        "previousAttempts": [],
        "id": "aaaaa-aaaaaa-aaaa-aaa",
        "parentId": null,
        "issues":[
          {
              "type": "warning",
              "category": "General"
          },
          {
              "type": "warning",
              "category": "General"
          }
        ]
    }
  ]
}
"""

data_model = DataModel.parse_raw(data)
print(data_model.dict())

You can find addition documentation here: https://pydantic-docs.helpmanual.io/ Also it contain installation steps.

CodePudding user response:

This should work for you

import json

data = json.loads(json_data)

issues = [r.get('issues', []) for r in data['records']]
  • Related