Python Forum
Split of key from JSON response
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.
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Parse Nested JSON String in Python mmm07 4 1,428 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,191 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  Name not found in response json NewbiePyt 4 1,019 Dec-29-2022, 11:12 AM
Last Post: buran
  Python Split json into separate json based on node value CzarR 1 5,476 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  JSON response from REST service get nested value nl2ttl 2 2,489 Nov-30-2020, 09:34 PM
Last Post: nl2ttl
  parser json response absolut 4 2,784 Sep-15-2020, 12:10 PM
Last Post: buran
  Empty response to request causing .json() to error t4keheart 1 9,953 Jun-26-2020, 08:35 PM
Last Post: bowlofred
  [split] script: remove all "carriage return" from my json variable pete 2 2,745 May-05-2020, 03:22 PM
Last Post: deanhystad
  API JSON response missing list gives keyerror rolfmadsen 3 3,399 Mar-28-2020, 10:12 AM
Last Post: buran
  How to process JSON response from requests? Heinrich 5 3,961 Jan-10-2020, 06:17 PM
Last Post: Heinrich

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020