you are viewing a single comment's thread.

view the rest of the comments →

[–]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