简体   繁体   中英

JSON response with complex data Structure

I am trying to move from VBA to Python. I am trying to get the results passed to Excel.

[
    {
        "id": 1993966466,
        "deficiencyArea": {
            "id": 270424374,
            "area": "18",
            "areaDescription": "Labour conditions",
            "subArea": "4",
            "subAreaDescription": "Health protection, medical care, social security",
            "ismRelated": False
        },
        "defectiveItem": {
            "id": 273058079,
            "code": "18417",
            "description": "Anchoring devices",
            "restricted": False
        },
        "defaultDescription": {
            "id": 742,
            "code": "1047",
            "description": "Not ready for use"
        },
        "groundForDetention": False,
        "recognizedOrganizationRelated": False,
        "accidentalDamage": False,
        "ismRelated": False
    },
    {
        "id": 1993966468,
        "deficiencyArea": {
            "id": 156,
            "area": "07",
            "areaDescription": "Fire safety",
            "subArea": "1",
            "subAreaDescription": None,
            "ismRelated": False
        },
        "defectiveItem": {
            "id": 1358,
            "code": "07105",
            "description": "Fire doors/openings in fire-resisting divisions",
            "restricted": False
        },
        "defaultDescription": {
            "id": 470,
            "code": "1011",
            "description": "Not as required"
        },
        "groundForDetention": False,
        "recognizedOrganizationRelated": False,
        "accidentalDamage": False,
        "ismRelated": False
    }
]

The objective is to pass to Excel the following keys for each deficiency (one row per deficiency): 'areaDescription' , 'subAreaDescription', 'description' (this one is inside 'deficiencyArea'), 'restricted' and 'description' (this one inside 'defaultDescription').

I have tryed a few things but nothing seams to work. Any suggestions?

Some code to make a start with. It creates a new workbook and writes the values to rows.

import json
import openpyxl

data = [
    {
        "id": 1993966466,
        "deficiencyArea": {
            "id": 270424374,
            "area": "18",
            "areaDescription": "Labour conditions",
            "subArea": "4",
            "subAreaDescription": "Health protection, medical care, social security",
            "ismRelated": False
        },
        "defectiveItem": {
            "id": 273058079,
            "code": "18417",
            "description": "Anchoring devices",
            "restricted": False
        },
        "defaultDescription": {
            "id": 742,
            "code": "1047",
            "description": "Not ready for use"
        },
        "groundForDetention": False,
        "recognizedOrganizationRelated": False,
        "accidentalDamage": False,
        "ismRelated": False
    },
    {
        "id": 1993966468,
        "deficiencyArea": {
            "id": 156,
            "area": "07",
            "areaDescription": "Fire safety",
            "subArea": "1",
            "subAreaDescription": None,
            "ismRelated": False
        },
        "defectiveItem": {
            "id": 1358,
            "code": "07105",
            "description": "Fire doors/openings in fire-resisting divisions",
            "restricted": False
        },
        "defaultDescription": {
            "id": 470,
            "code": "1011",
            "description": "Not as required"
        },
        "groundForDetention": False,
        "recognizedOrganizationRelated": False,
        "accidentalDamage": False,
        "ismRelated": False
    }
]

# create workbook
wb = openpyxl.Workbook()
ws = wb.worksheets[0]

row1 = ["id","areaDescription","subAreaDescription","description","restricted","description"]
for i in range(1,7):
    ws.cell(1,i).value = row1[i-1]

i = 1
for rec in data:

    area = rec["deficiencyArea"]
    item = rec["defectiveItem"]    
    desc = rec["defaultDescription"]

    i += 1
    ws.cell(i,1).value = rec["id"]
    ws.cell(i,2).value = area["areaDescription"]
    ws.cell(i,3).value = area["subAreaDescription"]
    ws.cell(i,4).value = item["description"]
    ws.cell(i,5).value = item["restricted"]
    ws.cell(i,6).value = desc["description"]

filename = "result.xlsx"
wb.save(filename=filename)
print("{} created with {} rows".format(filename,i-1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM