Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Split of key from JSON response
#1
HI Team,

I get a json response of keys & values from api as below
'fm_data': {'LineofBusiness~PoleBranch~Region~MappedName~MappedNameSub~Owner~::~PMC~APAC~China~PMC-CHINA~PMC-CHINA~Leo Pan': {'aggregate': {'fc0_crm': 206000.0,
'fcn0_crm': 107000.0,
'fcn2_crm': 313000.0,
'fcPipe_crm': 50000.0},
'PMC ModSys': {'fc0_crm': 195000.0,
'fcn0_crm': 107000.0,
'fcn2_crm': 302000.0},
'PMC FI / PI': {'fc0_crm': 11000.0,
'fcn2_crm': 11000.0,
'fcPipe_crm': 50000.0}},
'LineofBusiness~PoleBranch~Region~MappedName~::~SME~APAC~India~SME-India': {'aggregate': {'fc0_crm': 622836.6,
'fcn0_crm': 213587.0,
'fcn2_crm': 622836.6},
'SME Electric': {'fc0_crm': 43300.3,
'fcn0_crm': 213587.0,
'fcn2_crm': 43300.3},
'SME Gas': {'fc0_crm': 579536.2,
'fcn0_crm': 214302.5,
'fcn2_crm': 579536.2}},
'LineofBusiness~PoleBranch~Region~MappedName~MappedNameSub~Owner~::~HTS~APAC~India~HTS-IND West~HTS-IND West~Vivekananda Majumder': {'aggregate': {'fcn0_crm': 100000.0,
'fcn2_crm': 100000.0},
'HTS ETO': {'fcn0_crm': 100000.0, 'fcn2_crm': 100000.0}},
'LineofBusiness~PoleBranch~Region~MappedName~::~HCP~AMER~CSAR~HCP-Andean/Caribbean': {'aggregate': {'fc0_crm': 522928.4,
'fcn0_crm': 248137.7,
'fcn2_crm': 788916.4,
'fcPipe_crm': 308597.7,
'fc8_crm': 17850.2},
'HCP Software': {'fc0_crm': 33922.2,
'fcn0_crm': 20000.0,
'fcn2_crm': 53922.2,
'fcPipe_crm': 300023.0},
'HCP Base': {'fc0_crm': 489006.2,
'fcn0_crm': 228137.7,
'fcn2_crm': 734994.1,
'fcPipe_crm': 8574.7,
'fc8_crm': 17850.2}}}

I need to get a output like(Delimeter is ~ & ~::~) which need to be separated to different column:
LineofBusiness PoleBranch Region MappedName MappedNameSub Owner LOB Type value
PMC APAC China PMC-CHINA PMC-CHINA Leo Pan
SME APAC India SME-India Null Null
.....

for each value as rows.

Thanks in advance.
Quote
#2
The data's already in a dictionary. It can be read directly
All you need to do is add your formatting

example:
zz = {
'fm_data': {'LineofBusiness~PoleBranch~Region~MappedName~MappedNameSub~Owner~::~PMC~APAC~China~PMC-CHINA~PMC-CHINA~Leo Pan': {'aggregate': {'fc0_crm': 206000.0,
'fcn0_crm': 107000.0,
'fcn2_crm': 313000.0,
'fcPipe_crm': 50000.0},
'PMC ModSys': {'fc0_crm': 195000.0,
'fcn0_crm': 107000.0,
'fcn2_crm': 302000.0},
'PMC FI / PI': {'fc0_crm': 11000.0,
'fcn2_crm': 11000.0,
'fcPipe_crm': 50000.0}},
'LineofBusiness~PoleBranch~Region~MappedName~::~SME~APAC~India~SME-India': {'aggregate': {'fc0_crm': 622836.6,
'fcn0_crm': 213587.0,
'fcn2_crm': 622836.6},
'SME Electric': {'fc0_crm': 43300.3,
'fcn0_crm': 213587.0,
'fcn2_crm': 43300.3},
'SME Gas': {'fc0_crm': 579536.2,
'fcn0_crm': 214302.5,
'fcn2_crm': 579536.2}},
'LineofBusiness~PoleBranch~Region~MappedName~MappedNameSub~Owner~::~HTS~APAC~India~HTS-IND West~HTS-IND West~Vivekananda Majumder': {'aggregate': {'fcn0_crm': 100000.0,
'fcn2_crm': 100000.0},
'HTS ETO': {'fcn0_crm': 100000.0, 'fcn2_crm': 100000.0}},
'LineofBusiness~PoleBranch~Region~MappedName~::~HCP~AMER~CSAR~HCP-Andean/Caribbean': {'aggregate': {'fc0_crm': 522928.4,
'fcn0_crm': 248137.7,
'fcn2_crm': 788916.4,
'fcPipe_crm': 308597.7,
'fc8_crm': 17850.2},
'HCP Software': {'fc0_crm': 33922.2,
'fcn0_crm': 20000.0,
'fcn2_crm': 53922.2,
'fcPipe_crm': 300023.0},
'HCP Base': {'fc0_crm': 489006.2,
'fcn0_crm': 228137.7,
'fcn2_crm': 734994.1,
'fcPipe_crm': 8574.7,
'fc8_crm': 17850.2}}}
}

def display_dict(dictname, level=0):
    indent = " " * (4 * level)
    for key, value in dictname.items():
        if isinstance(value, dict):
            print(f'\n{indent}{key}')
            level += 1
            display_dict(value, level)
        else:
            print(f'{indent}{key}: {value}')
        if level > 0:
            level -= 1

if __name__ == '__main__':
    display_dict(zz)
results of running above script (I put in code tags for scrolling)
fm_data
    LineofBusiness~PoleBranch~Region~MappedName~MappedNameSub~Owner~::~PMC~APAC~China~PMC-CHINA~PMC-CHINA~Leo Pan

        aggregate
            fc0_crm: 206000.0
            fcn0_crm: 107000.0
            fcn2_crm: 313000.0
            fcPipe_crm: 50000.0

        PMC ModSys
            fc0_crm: 195000.0
            fcn0_crm: 107000.0
            fcn2_crm: 302000.0

        PMC FI / PI
            fc0_crm: 11000.0
            fcn2_crm: 11000.0
            fcPipe_crm: 50000.0

    LineofBusiness~PoleBranch~Region~MappedName~::~SME~APAC~India~SME-India

        aggregate
            fc0_crm: 622836.6
            fcn0_crm: 213587.0
            fcn2_crm: 622836.6

        SME Electric
            fc0_crm: 43300.3
            fcn0_crm: 213587.0
            fcn2_crm: 43300.3

        SME Gas
            fc0_crm: 579536.2
            fcn0_crm: 214302.5
            fcn2_crm: 579536.2

    LineofBusiness~PoleBranch~Region~MappedName~MappedNameSub~Owner~::~HTS~APAC~India~HTS-IND West~HTS-IND West~Vivekananda Majumder

        aggregate
            fcn0_crm: 100000.0
            fcn2_crm: 100000.0

        HTS ETO
            fcn0_crm: 100000.0
            fcn2_crm: 100000.0

    LineofBusiness~PoleBranch~Region~MappedName~::~HCP~AMER~CSAR~HCP-Andean/Caribbean

        aggregate
            fc0_crm: 522928.4
            fcn0_crm: 248137.7
            fcn2_crm: 788916.4
            fcPipe_crm: 308597.7
            fc8_crm: 17850.2

        HCP Software
            fc0_crm: 33922.2
            fcn0_crm: 20000.0
            fcn2_crm: 53922.2
            fcPipe_crm: 300023.0

        HCP Base
            fc0_crm: 489006.2
            fcn0_crm: 228137.7
            fcn2_crm: 734994.1
            fcPipe_crm: 8574.7
            fc8_crm: 17850.2
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [split] Print JSON Dictionary to Excel? venukommu 1 176 Nov-15-2019, 09:33 PM
Last Post: micseydel
  [split] Reading json from webpage rajesh1997 1 304 Feb-05-2019, 12:07 PM
Last Post: buran
  [split] How to insert JSON into MSSQL gehrenfeld 3 525 Jan-27-2019, 06:20 PM
Last Post: micseydel
  [split] Json to CSV satish 5 1,652 Jul-12-2017, 03:30 PM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)