I'm reading the contents of a JSON text file and am struggling to get the contents of a specific index in the data.
An example of the data objects I'm reading is below. There are multiple instances in my file, but they all look similar to the following:
[{ "value": "hello", "name": "janedoe", "istrue": "yes", "number": 5 }]
The console returns nothing every time and I can print json_list[0] and it returns the whole value {'name': 'janedoe', 'number': 5}.
I would like to use the substring "doe" to search within the list and find a match and then return the index of that match.
I have tried using a function and one liners such as this
res = [i for i in json_list if substring in i]
with open ('text.txt', 'r') as output_file:
json_array = json.load(output_file)
json_list = []
json_list = [{'name': item['name'].split('.')[0], 'number': item['number']}
for item in json_array]
substring = 'doe'
def index_containing_substring(json_list, substring):
for i, s in enumerate(json_list):
if substring in s:
return i
return -1
I would like to return the index value so that I can then call that index and utilize its data.