Home > Software engineering >  python nested json loop
python nested json loop

Time:04-29

pls. help me to get nested json data record. with below code i can get only one column but i need one record,

import json
response = """{
    "Code": "0",
    "Data": [
        {
            "BEGTAG": ""
        },
        {
            "ACCNo": "02010108800014",
            "AccountName": "PRATIKSHYA THAPA",
            "AvailableBalance": "75667.71",
            "BEGTAG": "",
            "Category": "6001",
            "CategoryDesc": "Mero Bachat Khata",
            "CrIntRate": "3.55%",
            "CustID": "10108800",
            "DrIntRate": "0%",
            "ENDTAG": "#END#,"
        },
        {
            "ACCNo": "00210018662013",
            "AccountName": "PADAM RANA",
            "AvailableBalance": "5571.65",
            "BEGTAG": "",
            "Category": "6002",
            "CategoryDesc": "Savings Staff",
            "CrIntRate": "4.55%",
            "CustID": "10018662",
            "DrIntRate": "0%",
            "ENDTAG": "#END#,"
        },
        {
            "ACCNo": "00210018662021",
            "AccountName": "PADAM RANA-HOME LOAN REPAYMENT",
            "AvailableBalance": "108837.52",
            "BEGTAG": "",
            "Category": "6007",
            "CategoryDesc": "Staff Repay Fund",
            "CrIntRate": "4%",
            "CustID": "10018662",
            "DrIntRate": "0%",
            "ENDTAG": "#END#"
        }
    ],
    "Message": "Success"
}"""

response = json.loads(response)

def json_extract(obj, key):
    arr = []
    def extract(obj, arr, key):
        if isinstance(obj, dict):
            for k, v in obj.items():
                if isinstance(v, (dict, list)):
                    extract(v, arr, key)
                elif k == key:
                    arr.append(v)
        elif isinstance(obj, list):
            for item in obj:
                extract(item, arr, key)
        return arr

    values = extract(obj, arr, key)
    return values


r = json_extract(response, 'ACCNo')

print(r)

I need a dictionary or list to have the record.

CodePudding user response:

Hope this helps you get more than one column:

import json
response = """{
            "Code": "0",
            "Data": [
                {
                    "BEGTAG": ""
                },
                {
                    "ACCNo": "02010108800014",
                    "AccountName": "PRATIKSHYA THAPA",
                    "AvailableBalance": "75667.71",
                    "BEGTAG": "",
                    "Category": "6001",
                    "CategoryDesc": "Mero Bachat Khata",
                    "CrIntRate": "3.55%",
                    "CustID": "10108800",
                    "DrIntRate": "0%",
                    "ENDTAG": "#END#,"
                },
                {
                    "ACCNo": "00210018662013",
                    "AccountName": "PADAM RANA",
                    "AvailableBalance": "5571.65",
                    "BEGTAG": "",
                    "Category": "6002",
                    "CategoryDesc": "Savings Staff",
                    "CrIntRate": "4.55%",
                    "CustID": "10018662",
                    "DrIntRate": "0%",
                    "ENDTAG": "#END#,"
                },
                {
                    "ACCNo": "00210018662021",
                    "AccountName": "PADAM RANA-HOME LOAN REPAYMENT",
                    "AvailableBalance": "108837.52",
                    "BEGTAG": "",
                    "Category": "6007",
                    "CategoryDesc": "Staff Repay Fund",
                    "CrIntRate": "4%",
                    "CustID": "10018662",
                    "DrIntRate": "0%",
                    "ENDTAG": "#END#"
                }
            ],
            "Message": "Success"
        }"""
response = json.loads(response)

data = response["Data"][1:]

for record in data:
    print(record['ACCNo'],"|", record['AccountName'],"|", record['AvailableBalance'])
  • Related