Finding the JSON file containing a particular key value , among multiple JSON files

So, I have 5 JSON files named as JSON1,JSON2,JSON3,JSON4 and JSON5. General format of these json files is

General format of JSON files

{
  "flower": {
    "price": {
      "type": "good",
      "value": 5282.0,
      "direction": "up"
     }
    },
  "furniture": {
    "price": {
      "type": "comfy",
      "value": 9074.0,
      "direction": "down"
     }
   }
}

Among all these json files , I need to find that json file which has a particular value/data given by the user. for eg- if the user gives an input for the “value” = 9074 that it want to search in all the JSON files and gives an output as the JSON file which contains the value mentioned along with the line which has the mentioned data.

The approach I used was:

import json
with open('JSON1.json','r') as f1:
    item1 = json.load(f1)
with open('JSON2.json','r') as f1:
    item2 = json.load(f1)
with open('JSON3.json','r') as f1:
    item3 = json.load(f1)
with open('JSON4.json','r') as f1:
    item4 = json.load(f1)
with open('JSON5.json','r') as f1:
    item5 = json.load(f1)
# Input the value that user want to search
item = input("Enter the value:n")

# function to search the json file
def search_json(value):
 int i = 0;
  for keyvalue in item(i+1):
    i++;
    if value == keyval['value']
      return keyvalue[name of the json file]

if(search_json(item) !=None):
 print("this value is present in json log :",search_json(item))

the output observed should be:

Enter the value: 9074

This value is present in json log JSON1 and is present at 12 line of code

But, the above approach isn’t efficient and correct . As, I’m new to learning python therefore I’m confused with the correct approach. It’ll be really grateful if someone could help.

Answer

Loop through the filenames.

def find_value_in_json_files(value, files):
    for f in files:
        with open(f) as f:
            item = json.load(f)
        for k, v in item.items():
            price = v['price']
            if price.get('value', price.get('values') == value:
                return f, k

print(find_value_in_json_files(9074, ['JSON1.json', 'JSON2.json', 'JSON3.json', 'JSON4.json', 'JSON5.json']))