首页 > 解决方案 > 在 Python 中提取所有父/子 Json 值

问题描述

下面是输入的 Json 模式和预期的输出响应 \n

输入模式:{“request”:{“requestId”:“ABCDE”,“modelType”:“NEW”,“lineOfBusiness”:“WC”,“requestDate”:“2019-12-10”,“evaluationDate”:“ 2019-12-11”、“jobNumber”:“GDERS”、“branchNumber”:“IODGH”、“quoteEffectiveDate”:“2019-12-11”、“policyNumber”:“12XYZ34”、“policyEffectiveDate”:“2019- 12-10”,“policyExpirationDate”:“2020-12-09”},“premiumHistory”:[{“jobNumber”:“GDERS”,“branchNumber”:“IODGH”、“policyNumber”:“”、“policyEffectiveDate”:“2019-04-01”、“policyExpirationDate”:“2020-03-31”、“classCode”:[{“classCode”:“AB”、“ transactionDate”:“2019-04-01”,“transactionType”:“1”,“locationNumber”:“12345”,“premium”:1200,“payrollOrExposure”:100 },“2020-03-31”,“classCode”:[{“classCode”:“AB”,“transactionDate”:“2019-04-01”,“transactionType”:“1”,“locationNumber”:“12345”, “溢价”:1200,“payrollOrExposure”:100 },“2020-03-31”,“classCode”:[{“classCode”:“AB”,“transactionDate”:“2019-04-01”,“transactionType”:“1”,“locationNumber”:“12345”, “溢价”:1200,“payrollOrExposure”:100 },“溢价”:1200,“payrollOrExposure”:100 },“溢价”:1200,“payrollOrExposure”:100 },

                {
                "classCode":"CD",               
                "transactionDate": "2019-04-01",
                "transactionType": "1",
                "locationNumber": "12345",
                "premium": 1200,
                "payrollOrExposure": 100
                }
            ]

        },
        ]
}

Output Expected:
request.requestId = "ABCDE"
request.modelType = "NEW"
..
..
..
..
premiumHistory.jobNumber="GDERS"
premiumHistory.branchNumber="IODGH"
premiumHistory.policyNumber=""
..
premiumHistory.classCode.classCode="AB"
premimumHistory.classCode.transactionDate="2019-04-01"
..
..
..

代码需要在 python 中,我已经使用不同的技术进行了测试,但无法获得所需的输出。非常感谢任何帮助。

问候,

标签: pythonjson

解决方案


对于格式问题,我深表歉意。甚至没有意识到这一点。

下面是输入的 Json 模式和预期的输出响应 \n

输入模式:

{
    "request": {
        "requestId": "ABCDE",
        "modelType": "NEW",
        "lineOfBusiness": "WC",
        "requestDate": "2019-12-10",
        "evaluationDate": "2019-12-11",
        "jobNumber": "GDERS",
        "branchNumber": "IODGH",
        "quoteEffectiveDate": "2019-12-11",
        "policyNumber": "12XYZ34",
        "policyEffectiveDate": "2019-12-10",
        "policyExpirationDate": "2020-12-09"
    },
    "premiumHistory": [{
        "jobNumber": "GDERS",
        "branchNumber": "IODGH",
        "policyNumber": "",
        "policyEffectiveDate": "2019-04-01",
        "policyExpirationDate": "2020-03-31",
        "classCode":
            [
                {
                "classCode":"AB",
                "transactionDate": "2019-04-01",
                "transactionType": "1",
                "locationNumber": "12345",
                "premium": 1200,
                "payrollOrExposure": 100
                },

                {
                "classCode":"CD",               
                "transactionDate": "2019-04-01",
                "transactionType": "1",
                "locationNumber": "12345",
                "premium": 1200,
                "payrollOrExposure": 100
                }
            ]

        },
        ]
}

Output Expected:
request.requestId = "ABCDE"
request.modelType = "NEW"
..
..
..
..
premiumHistory.jobNumber="GDERS"
premiumHistory.branchNumber="IODGH"
premiumHistory.policyNumber=""
..
premiumHistory.classCode.classCode="AB"
premimumHistory.classCode.transactionDate="2019-04-01"
..
..
..

推荐阅读