Search for Data in JSON Object using Python

Python supports the processing of JSON data natively using json module. The package can handle string-formatted JSON data or JSON data saved in a file.

If JSON data is string-formatted, searching through it involves two steps: converting the string into a Python object using json.loads(), then search the resulting Python object accordingly.

On the other hand, if JSON data is saved on a file, searching involves one additional step at the beginning – that of loading the file.

Searching for Data in a JSON String

As said earlier, we need to convert the JSON string into a valid Python object and search for the value we want.

Example 1: Search key and value in simple JSON data

Output:

Key value not found
ID value found.

The simple example above checks if a given key exists and if the JSON data contains a given value for a given key.

We can access dictionary values in two ways: using <dict>.get(<key>) or <dict>[<key>]. The former returns value if the <key> exists, otherwise returns None, whereas the latter raises a ValueError if the <key> does not exist.

Example 2: Searching for a value in a JSON string (3 methods discussed)

The following code initializes a JSON string and converts it into a Python list.

At this point, data1 is a Python object – a list of 3 dictionaries. We can then search for values as we would on an ordinary Python list of dictionaries.

Method A: Searching through iteration

In this method, we need to iterate through the elements of data1 and search for a value in each dictionary.

Output:

{'ID': 'AM444', 'Name': 'Paul', 'Address': 'Minnesota'}

Method B: By list comprehension

This method works like Method A. We are just collapsing the for-loop into a list comprehension.

Output:

[{'ID': 'AM444', 'Name': 'Paul', 'Address': 'Minnesota'}]

Method C: Using lambda and filter function

The filter(func, iterable) evaluates each iterable and returns an iterator of items matching the criteria defined by func. In our case, we will use lambda x: f(x) to define the func as follows

Output:

[{'ID': 'AM444', 'Name': 'Paul', 'Address': 'Minnesota'}]

We had to cast the filter object into a list because the filter() method returns an iterator object.

Example 3: Searching for data in a Nested JSON

The following code snippet parses a JSON string into a Python list of dictionaries.

Once the JSON data is loaded, we can search for values using any of the 3 methods discussed earlier.

Output:

{'ID': 'MT456', 'Name': 'Alice', 'Address': {'State': 'Ohio', 'County': 'Delaware'}}
[{'ID': 'MT456', 'Name': 'Alice', 'Address': {'State': 'Ohio', 'County': 'Delaware'}}]
[]
[{'ID': 'AM444', 'Name': 'Bob', 'Address': {'State': 'Minnesota', 'County': 'Dakota'}}]

We needed to search for “County” inside the “Address” key in all three methods. The first item on the JSON data has Address = “null”. If we issued item[“Address”][“County”] in this case, we would end up with an error because item[“Address”] yields a NoneType for the first element.

Searching for Data in a JSON file

The following data is saved in the address_data.json file. We will use it in the following example.

[
 {
   "ID": "KZ568",
   "Name": "Mark",
   "Address": null
 },
 {
   "ID": "MT456",
   "Name": "Vincent",
   "Address": "Ohio"
 },
 {
   "ID": "AM444",
   "Name": "Paul",
   "Address": "Minnesota"
 }
]

Loading the JSON file

Output:

[{'ID': 'KZ568', 'Name': 'Mark', 'Address': None}, {'ID': 'MT456', 'Name': 'Vincent', 'Address': 'Ohio'}, {'ID': 'AM444', 'Name': 'Paul', 'Address': 'Minnesota'}]

Once the data is loaded, you can search through it using any of the methods discussed earlier.

Conclusion

As you might have already noticed, there is no one specific method to search through any JSON data. The core idea is to understand the workings of the methods discussed in this article and modify the code to fit your specific needs.