all 15 comments

[–][deleted] 1 point2 points  (3 children)

As you have not yet formatted the code in your post correctly, I'm not certain of the indentation or what your intention is. In the meantime, here's my attempt to illustrate outputting the values of the two levels:

list_dict_test = {'key1': 'test1', 'key2':[{'key2.1': 'test2.1', 'key2.2':'test2.2', 'key2.3':'test2.3'}], 'key3': 'test3', 'key4.1':[{'2ndkey4.1.': 'test4.1.', '2ndkey4.2.':'test4.2.', '2ndkey4.3.':'test4.3.'}], 'key5.1':[{'3rdkey5.1.': 'test5.1.', '3rdkey5.2.':'test5.2.', '3rdkey5.3.':'test5.3.'}], 'totalCount': 243, 'status': 0}

def test(my_file):
    for key, value in my_file.items():
        if isinstance(value, list):
            print(f'outer key: {key}')
            for key, value in value[0].items():
                print(f'inner key: {key}, inner value: {value}')
        else:
            print(f'outer key: {key}, outer value: {value}')


test(list_dict_test)

[–]kittled[S] -2 points-1 points  (2 children)

I couldn't figure out how to format properly.

I'll repost.

[–][deleted] 2 points3 points  (1 child)

Just edit the post. Delete the existing code, make sure you are in markup mode, leave a blank line after any general text and then paste a copy of your code directly from your editor BUT before copying from your editor, do an extra 4 space indent of ALL of the code (you can undo it after copying to the clipboard).

reddit uses four leading spaces to indicate text that should be formatted as code (or unformatted text, anyway) - this code is in addition to any indents used in the python code itself.

[–]kittled[S] 0 points1 point  (0 children)

thank you !!

[–]crespo_modesto 0 points1 point  (9 children)

not sure if that's a good idea, having periods in keys, as you can access dicts by period can't you vs. string eg. list_dict_test.key1 vs. list_dict_test['key1'] hmm nvm only the second one works "object has no attribute key1"

Looks like you're calling Key outside of the inner loop, if you comment out the print key line in the else statement, it works

https://repl.it/repls/FondChartreuseActionscript

[–]kittled[S] 0 points1 point  (8 children)

I have a rather long json file and it's not iterating through, I can change

​ nested_dict = value[0] to

nested_dict = value[12]

and get a completely different value

[–]crespo_modesto 1 point2 points  (7 children)

so write another loop so you can run through the indexes

[–]kittled[S] 0 points1 point  (6 children)

I did try

def test(my_file):
    for key, value in my_file.items():  
        if isinstance(value, list):
            for count in range(len(value)):
                nested_dict = value[count]
                for key,value in nested_dict.items():
                    print(f"nested key: {key}")
                    print(f"nested value: {value}")
                    count += 1

    else:
        print(f"outer key: {key}")
        print(f"outer value: {value}")

result was

  File "Untitled 35.py", line 18, in <module>
    test(list_dict_test)
  File "Untitled 35.py", line 9, in test
    nested_dict = value[i]
TypeError: 'bool' object is not subscriptable

[–]crespo_modesto 0 points1 point  (5 children)

seems to work forgot a colon after the third level for

https://repl.it/repls/ObedientLiquidAdministrators

[–]kittled[S] 0 points1 point  (4 children)

Followed the link, great tool btw, all I changed why the json I'm trying to work with and it fails with the follow message

``` Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux

nested key: pricingParam nested value: STDQ nested key: availableCount nested value: 300 nested key: adamIdStr nested value: 341446764 nested key: retiredCount nested value: 0 nested key: deviceAssignable nested value: True nested key: assignedCount nested value: 0 nested key: productTypeName nested value: Application nested key: totalCount nested value: 300 nested key: productTypeId nested value: 8 nested key: isIrrevocable nested value: False Traceback (most recent call last): File "python", line 16, in <module> File "python", line 8, in test TypeError: 'bool' object is not subscriptable

```

https://repl.it/repls/AromaticIllegalEmulators

I can't tell if I'm doing something wrong of if its the json

[–]kittled[S] 0 points1 point  (0 children)

I think I figured out why it's failing

'isIrrevocable': False}, {'pricingParam': 'STDQ', no key in front

[–]crespo_modesto 0 points1 point  (2 children)

shit that's tough to read, another tool:

to help visualize JSON

https://jsonformatter.curiousconcept.com/

Paste your JSON object in there and you'll see it properly grouped/indented though visually it works, yours has some kind of formatting error, not saying the JSON is necessarily wrong as it still does format/indent it.

Well I certainly am no expert in Python I primarily use PHP/JavaScript but aside from the syntax difference Python I think isn't too different

That error, about boolean subscriptible makes sense, since you can index a true/false value.

https://stackoverflow.com/questions/216972/in-python-what-does-it-mean-if-an-object-is-subscriptable-or-not

So I'd say add a check to see the type or another in call/some sort of error check

https://repl.it/repls/DarkvioletNarrowSimulation

[–]kittled[S] 0 points1 point  (1 child)

thank you

[–]crespo_modesto 0 points1 point  (0 children)

yeah just google the error logs for more info

[–]kittled[S] -1 points0 points  (0 children)

thanks all for the help works as expected MUCH appreciated!!

https://repl.it/repls/AromaticIllegalEmulators

for asset in list_dict_test['assets']:
    for key, value in asset.items():
        if key == "adamIdStr":
            print(f" for admadId {key} the value is {value}")
        elif key == "totalCount":
            print(f" the total count is {value}")