0

Using python, Trying to parse through every key value within the dictionary. I was able to parse key value where the value is in turn another dictionary. But I'm now stuck at a point to parse a list within that inner dictionary. Below is the JSON structure.

json_struct = {
    "Name":"John",
    "Age":"30",
    "State":"NC",
    "xxxx":{
        "xxxx1":"1111111",
        "xxxx2":"222222",
        "xxxx3":[
            {
                "aaa1": "333",
                "aaa2":"444"
            },
            {
                "bbb1": "555",
                "bbb2":"666"
            }
        ]
    }
}

Code piece doing the iteration:

def check1(json_struct):
    for k, v in json_struct.items():
        if isinstance(v, OrderedDict):
            check1(v)
        else:
            print "{0} : {1}".format(k, v)

Actual Output:

Name:John
Age:30
state:NC
xxxx1:1111111
xxxx2:222222
xxxx3:[('aaa1','333'), ('aaa2':'444'), ('bbb1:555'), ('bbb2:666')]

Expected Output:

Name:John
Age:30
state:NC
xxxx1:1111111
xxxx2:222222
aaa1:333
aaa2:444
bbb1:555
bbb2:666

I'm missing something to iterate through the list i believe, but I tried the isinstance with list within the if as well, still the incorrect result is what I'm getting.

Any knowledge share on this will be highly appreciated.

Thanks in advance!

1 Answer 1

1

There's two issues:

  • you're checking for the type OrderedDict but this is probably specific to the version of Python you're using; on Python 3.x you'd check for dict instead;

  • the aaa1 and similar values are inside of a list, between [] and that's what you're seeing as the output.

This is what you'd do to check for that too:

def check1(json_struct):
    for k, v in json_struct.items():
        if isinstance(v, OrderedDict):
            check1(v)
        elif isinstance(v, list):
            for e in v:
                check1(e)
        else:
            print "{0} : {1}".format(k, v)

You may need dict instead of OrderedDict, when using versions of Python 3.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm using Python 2.7. And the reason for OrderedDict is to maintain the order of similar to key/value in JSON. Using dict, changes the order in the result set. But yes when I use dict, it works fine but it is just that order is changes. Another quick question, When iterating through list within dict, there are some unicode values(not key/value). i.e {'"a":{"b":["0","1"]}}. This results in an error "AttributeError: 'unicode' object has no attribute 'items". Trying to find a specific function or check to handle this case.
Do you need to use Python 2? Because using Unicode is a lot simpler in Python 3 and your code would probably be fairly simple to update to work with Python 3 as well. If you do need Python 2, I don't understand your example, it seems to be missing a ' and the error message seems to suggest the json_struct you're passing in is a Unicode string unicode, instead of an OrderedDict?
Yes Grismar! I'll be sticking to 2.7 for this use case. Ignore the previous example, consider the below { "X":"Text1", "Y":"Text2", "a":{ "b":["0","1"] } } X and Y are parsed but when its comes to b, the code fails with "AttributeError: 'unicode' object has no attribute 'items". I also tried this, just to check how the code behaves,(not sure if it makes any difference) { "X":"Text1", "Y":"Text2", "b":["0","1"] } It is the same error. But now it fails immediately. Does not parse through X and Y.
The error about not having an attribute items would be because whatever gets passed in is not a dict (or OrderedDict). In your case, it seems like you're passing in a Unicode string. Can you please update your original question with a clear example that has this behaviour? Because from what you're saying and from the code I provided, I cannot reproduce your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.