3

I have a json below, and I want to parse out value from this dict.

I can do something like this to get one specific value

print(abc['everything']['A']['1']['tree']['value'])

But, what is best way to parse out all "value?" I want to output good, bad, good.

   abc = {'everything': {'A': {'1': {'tree': {'value': 'good'}}}, 

'B': {'5': {'tree1': {'value': 'bad'}}},

'C': {'30': {'tree2': {'value': 'good'}}}}}
5
  • 1
    That's not JSON, that's regular Python dictionaries. Commented Jan 28, 2022 at 6:57
  • 1
    What have you tried so far? Commented Jan 28, 2022 at 6:58
  • 1
    If they are consistent, you can loop through the first key of each nested dictionary until you get one with key value then return its value, repeat. Commented Jan 28, 2022 at 7:07
  • like larry said just loop through each, check if value is not a dictionary type -> you get the results Commented Jan 28, 2022 at 7:09
  • This is dirty 1-liner if your structure is consistent [v[0][0][0] for v in [[[list(l3.values()) for l3 in l2.values()] for l2 in l1.values()] for l1 in abc['everything'].values()]] Commented Jan 28, 2022 at 7:11

3 Answers 3

2

If you are willing to use pandas, you could just use pd.json_normalize, which is actually quite fast:

import pandas as pd
 
abc = {'everything': {'A': {'1': {'tree': {'value': 'good'}}}, 

'B': {'5': {'tree1': {'value': 'bad'}}},

'C': {'30': {'tree2': {'value': 'good'}}}}}

df = pd.json_normalize(abc)
print(df.values[0])
['good' 'bad' 'good']

Without any extra libraries, you will have to iterate through your nested dictionary:

values = [abc['everything'][e][k][k1]['value'] for e in abc['everything'] for k in abc['everything'][e] for k1 in abc['everything'][e][k]]
print(values)
['good', 'bad', 'good']
Sign up to request clarification or add additional context in comments.

Comments

0

Provided your keys and dictionaries have a value somewhere, you can try this:

  • Create a function (or reuse the code) that gets the first element of the dictionary until the value key exists, then return that. Note that there are other ways of doing this.
  • Iterate through, getting the result under each value key and return.
# Define function
def get(d):
    while not "value" in d:
        d = list(d.values())[0]
    return d["value"]

# Get the results from your example
results = [get(v) for v in list(abc["everything"].values())]
['good', 'bad', 'good']

Comments

0

Firstly, the syntax you are using is incorrect.

If you are using pandas, you can code like

import pandas as pd

df4 = pd.DataFrame({"TreeType": ["Tree1", "Tree2", "Tree3"], "Values": ["Good", "Bad","Good"]})

df4.index = ["A","B","C"]

next just run the code df4, you would get the correct output.

output:

TreeType    Values

A Tree1 Good B Tree2 Bad C Tree3 Good

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.