skip to Main Content

I am trying to merge "n" number of json files but i need to first load json from filepath and then use it. I referred below link but they already have json data available but in my case i want to first load json data in loop and then merge it. I am looking for generic way of doing it.

How do I merge a list of dicts into a single dict?

My code: I want to first load json data for all the files and then merge it. I am trying something like below but it does not work and give error as TypeError: str , bytes or ospath.like object, not Tuple

def merge_JsonFiles(*file):
   result = {}
   for f in file:
     json_data = loadjson(file)
     result = {**json_data}
   return result

   merge_JsonFiles(file1, file2, file3)

def loadjson(file)
     with open(file , 'r') as fh:
       return json.load(fh)

Merge logic for two files = {**file1, **file2)

4

Answers


  1. Chosen as BEST ANSWER

    I resolved it using below code

    def merge_JsonFiles(*file):
       result = {}
       for f in file:       
         result = {**result, **loadjson(f)}
       return result
    

  2. You send tuple type in open function and reasonably get and error. The problem is in merge_JsonFiles funtion in t line json_data = loadjson(file),there instead of loadjson(file) you need to use loadjson(f), and then it’s all gonna work.

    Login or Signup to reply.
  3. It looks like there are a few issues with your code. Let’s go through them and provide a corrected version of the merge_JsonFiles function:

    1. The loadjson function is missing a colon (:) at the end of its definition.
    2. The merge_JsonFiles function is not using the loadjson function correctly. You should pass the file path (f) to the loadjson function instead of passing the tuple of all files (file).
    3. The merging logic is incorrect. Instead of repeatedly overwriting the result dictionary, you should be updating it with each loaded JSON data.

    Here’s the corrected version of your code:

    import json
    
    def merge_JsonFiles(*files):
        result = {}
        for f in files:
            json_data = loadjson(f)
            result.update(json_data)
        return result
    
    def loadjson(file):
        with open(file, 'r') as fh:
            return json.load(fh)
    
    # Example usage:
    merged_data = merge_JsonFiles("file1.json", "file2.json", "file3.json")
    print(merged_data)
    

    In this corrected version, the merge_JsonFiles function now takes a variable number of file paths (*files) and iterates through each file. It loads the JSON data from each file using the loadjson function and then updates the result dictionary with the loaded JSON data using the update() method. This way, the data from all files will be merged into a single dictionary.

    Login or Signup to reply.
  4. import json
    
    def loadjson(file):
        with open(file , 'r') as fh:
            return json.load(fh)
    
    def merge_JsonFiles(*files):
        result = {}
        for f in files:
            json_data = loadjson(f)
            result = {**result, **json_data}
        return result
    
    merge_JsonFiles('file1.json', 'file2.json', 'file3.json')
    

    In this code, merge_JsonFiles function takes an arbitrary number of file paths as arguments. It then loads each JSON file one by one using the loadjson function and merges them into one dictionary. Finally, the merged dictionary is returned.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search