0

My assignment is requiring me to write code that can handle finding elements from one json list in another json list. Here is an example of the data:

data_to_find = {'location': {'state': 'WA'}, 'active': True}
data_to_look_in = {'id': 3, 'last': 'Black', 'first': 'Jim', 'location': {'city': 'Spokane', 'state': 'WA', 'postalCode': '99207'}, 'active': True}
print(data_to_find)
print(data_to_look_in)

output:

{'location': {'state': 'WA'}, 'active': True}

{'id': 3, 'last': 'Black', 'first': 'Jim', 'location': {'city': 'Spokane', 'state': 'WA', 'postalCode': '99207'}, 'active': True}

I have to find to find the data in data_to_find in data_to_look_in and I am having a lot of trouble doing this. Here is what I have tried and what it returns:

for line in data_to_find:
  tmp1 = data_to_find[line]
  tmp2 = data_to_look_in[line]
  print(tmp1)
  print(tmp2)
  if tmp1 in tmp2:
    print("found")

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-46-cf3b325f53e8> in <module>()
      4   print(tmp1)
      5   print(tmp2)
----> 6   if tmp1 in tmp2:
      7     print("found")

TypeError: unhashable type: 'dict'

I've tried other ways that seem to return the same error so I am looking for help on how to best approach and solve this issue. Any tips would be appreciated. Thank you. EDIT: I am trying to write this so it can be done generically not just for these specific data values.

  • 1
    Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Rafael Setton Jun 03 '21 at 21:45
  • Not in particular because I am trying to write this in such a way that I can do it generically – Christian Cortez Jun 03 '21 at 21:48
  • That answer *does* show you the basics of nested searching. Your particular problem is that you tried to find an *entire dict* `tmp1` as a key in `tmp2`. This is not how you access dict entries. Please see the available tutorials and examples, as well as the marked link, to learn how to do this. – Prune Jun 03 '21 at 22:02
  • You need to search for a key, value pair: look up the key in `tmp2`, and then see whether the values match. – Prune Jun 03 '21 at 22:03

1 Answers1

0

Perhaps use Pandas:

import pandas as pd

data_to_find = {'location': {'state': 'WA'}, 'active': True}
data_to_look_in = {'id': 3, 'last': 'Black', 'first': 'Jim', 'location': {'city': 'Spokane', 'state': 'WA', 'postalCode': '99207'}, 'active': True}

df1 = pd.DataFrame.from_dict(data_to_find)
print(df1)
'''
      location  active
state       WA    True
'''

df2 = pd.DataFrame.from_dict(data_to_look_in)
print(df2)
'''
            id   last first location  active
city         3  Black   Jim  Spokane    True
postalCode   3  Black   Jim    99207    True
state        3  Black   Jim       WA    True
'''

search_results = pd.merge(df1, df2, how ='inner', on =['location', 'active'])
print(search_results)
'''
  location  active  id   last first
0       WA    True   3  Black   Jim
'''

If the length of search_results is greater than zero, you have one or more matches:

print(len(search_results))
'''
1
'''

This example merges on location and active columns. To solve this with a generic data_to_find object, where you don't know column names ahead of time, you might use on=data_to_find.keys(), using the top-level keys in that object. Those keys will need to be in the data_to_look_in object for the merge to work correctly.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • This returns errors sayiong that DataFrame is called incorrectly – Christian Cortez Jun 03 '21 at 22:35
  • Perhaps check that you are using current versions of Python (>=3.8) and Pandas (>=1.2) and update, if necessary. Otherwise, provide minimally-reproducible code for others to evaluate locally, if you can. This will help others help you. – Alex Reynolds Jun 03 '21 at 23:18