miu
miu

Reputation: 199

Find values from the Nested Dict

Something I'm doing incorrect, but I got data with list of nested dict, I need to pull the value and store in the output dict. Data:

[{
  "encoding":"json",
  "result": {
     "aclList":[
        {
           "chipName":"",
           "countersEnabled":true,
           "countersIncomplete":false,
           "dynamic":false,
           "name":"FW-RULE-IN",
           "readonly":false,
           "sequence":[
              {
                 "convertSymbols":true,
                 "sequenceNumber":1,
                 "text":"TEST: RULE 1"
              },
              {
                 "convertSymbols":true,
                 "sequenceNumber":2,
                 "text":"TEST: RULE 2"
              },
              {
                 "convertSymbols":true,
                 "sequenceNumber":3,
                 "text":"TEST: RULE 3"
              },]}]}}]

Like to put sequenceNumber, text values in to output result in Dict.

here is the code I got, but not working, how to fix it?

def parse_data(data):
    rules = {}
    acllist = []

    for k, v in data[0].items():
        for h, j in k['result'].items():
            for i in h['aclList'][0]:
                    for l in i[0]['sequence']:
                            rules['NUM'] = int(j['sequenceNumber'])
                            rules['TEXT_RULES'] = j['text']
                            acllist.append(rules.copy())
                            print (j['sequenceNumber'], j['text'])
return acllist

Upvotes: 0

Views: 27

Answers (1)

Axe319
Axe319

Reputation: 4365

I'm assuming there can be multiple elements to these lists. If there aren't you could simplify to.

for dct in data[0]['result']['aclList'][0]['sequence']:
    rules['NUM'] = int(dct['sequenceNumber'])
    rules['TEXT_RULES'] = dct['text']
    acllist.append(rules.copy())
    print (inner['sequenceNumber'], dct['text'])

However if you want to iterate through every list and gather the elements for each version, it would look more like this:

def parse_data(data):
    acllist = []
    
    # for each dict in the initial list
    for dct in data:
        # for each list in the ['aclList'] key of the ['result'] key
        for lst in dct['result']['aclList']:
            # for each inner dict in the ['sequence'] key
            for inner in lst['sequence']:
                rules = {}
                rules['NUM'] = int(inner['sequenceNumber'])
                rules['TEXT_RULES'] = inner['text']
                acllist.append(rules.copy())
                print (inner['sequenceNumber'], inner['text'])
    return acllist

data =[{
  "encoding":"json",
  "result": {
     "aclList":[
        {
           "chipName":"",
           "countersEnabled":True,
           "countersIncomplete":False,
           "dynamic":False,
           "name":"FW-RULE-IN",
           "readonly":False,
           "sequence":[
              {
                 "convertSymbols":True,
                 "sequenceNumber":1,
                 "text":"TEST: RULE 1"
              },
              {
                 "convertSymbols":True,
                 "sequenceNumber":2,
                 "text":"TEST: RULE 2"
              },
              {
                 "convertSymbols":True,
                 "sequenceNumber":3,
                 "text":"TEST: RULE 3"
              },]}]}}]
print(parse_data(data))

Where you went wrong is that you are iterating through the key/values of you dicts.

for k, v in data[0].items():

k here would contain "encoding" and "result". And then you are trying to iterate through those keys.

for h, j in k['result'].items():

But what you are essentially doing here is saying:

k = "result"
for h, j in k['result'].items():

Or:

for h, j in "result"['result'].items():

You can see why this would throw an error.

Upvotes: 1

Related Questions