1

I am trying to access the data in a JSON file by defining a list of query strings so that I can loop through them and dynamically extract the data.

In the example below, I trying to extract the "[1]['name']" and pass it as a variable.

   data_set = {1: [1, {'name':'bob'}, 3], 2: [1, {'name':'bill'}, 3],3: [1, {'name':'fred'}, 3]}

   for d in data_set:
       print(data_set[d][1]['name'])

I tried the following but it did not work:

    query_list = r'[1][''name'']'
    print(data_set[d][1]['name'])
4
  • Just to clarify. Do you have a string like "[1]['name']" which you want to use to traverse the dictionary / list in depth? Where is this string coming from? Commented May 27, 2020 at 6:32
  • I want to define the string. Actually it will be a list of strings and the actual JSON will be much complex. I want to be able to easily define the variables that I want to extract from it. Commented May 27, 2020 at 6:59
  • If it's you who defines the string, I'd rather use lists, like [1, 'name'] and then iterate over it. Commented May 27, 2020 at 7:06
  • Yes, a list like that could work as long as the depth is know. In some instances two levels but then in others 3 or 4 levels into the JSON are needed. Commented May 27, 2020 at 7:10

2 Answers 2

1

If you're in control of the strings, I'd rather use lists to represent paths, such as [1, 1, 'name']:

def get_nested(value, path):
    for p in path:
        value = value[p]
    return value

Then:

>>> data_set = {1: [1, {'name': 'bob'}, 3]}
>>> get_nested(data_set, [1, 1, 'name'])
>>> 'bob'

If you're more into functional style, you can also define get_nested() as:

import functools, operator
def get_nested(value, path):
    return functools.reduce(operator.getitem, path, value)

If you don't know the keys of data_set in advance, you can iterate over its values and pass them to get_nested():

for val in data_set.values():
    for query in queries:
        print(get_nested(val, query))
Sign up to request clarification or add additional context in comments.

3 Comments

Very close. Then I can loop through the following calling get_nested. queries = [[1, 1, 'name'],[1, 1, 'name'],[1, 3, 'city']] However, the first variable will "d" will be iterated over. Would you recommend me to send it to "get_nested" as a parameter or append it to the variable from queries before sending it?
@Justin if you don't know ds in advance, I'd just iterate over the original dict and pass the values to get_nested() as the first argument. I'll update the example.
Updating the first element of the list as I iterated over it worked well. Thanks for you help.
0

Firsly, the property names (in this case 1, 2, 3) should be enclosed in double quotes such as "1", "2" and "3"within your JSON data. Then use json.loads function along with a cursor containing an if condition to extract the desired value such as

import json
data_set = '{"1": [1, {"name":"bob"}, 3], "2": [1, {"name":"bill"}, 3],"3": [1, {"name":"fred"}, 3]}'

loaded_json = json.loads(data_set)
for i in loaded_json:
    if int(i) == 1:
        print( loaded_json[i] )

Agreed. Expanding on your code I am added an extra element to the JSON. I wanted to put the [1]['name'] and [3]['city'] into a string list so that I can loop through the list extracting data. The JSON will be much more complex and I will be extracting a large set of variables.

    data_set = '{"1": [1, {"name":"bob"}, 3,{"city":"London"}], "2": [1, {"name":"bill"}, 3,{"city":"Madrid"}],"3": [1, {"name":"fred"}, 3,{"city":"Portland"}]}'

    loaded_json = json.loads(data_set)
    for i in loaded_json:
          print(loaded_json[i][1]['name'])
          print(loaded_json[i][3]['city'])  

Comments

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.