Updating already existing json file python
<p>I have a json file and I want to update 'filename' field with the filenames I scp from a remote server. I am pretty new to python but learning as I go. </p>

<p>JSON file:</p>

<pre><code>{"path":"/home/Document/Python", 
"md5s":[{"filename":"",
    "md5":"",
    "timestamp":""},
   {"filename":"",
    "md5":"",
    "timestamp":""},
   {"filename":"",
    "md5":"",
    "timestamp":""}
]}
</code></pre>

<p>My python code so far: </p>

<pre><code>  def filemd5():
   try:
    config = json.load(open(config_file))
    #print(str(json.dumps(config, indent=4)))
    for server in config['servers']:
            ssh = SSHClient() 
            ssh.load_system_host_keys()

            ssh.connect(server['ip'], username=server['username'], 
                        password=server['password'])
        #print(str(server)) 
        print('Connecting to servers')
        ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('ls /tmp/')
        error = str(ssh_stderr.read())
        if len(error) ==0:
            for files in config['servers']: 
                filename = file_location + server['file']
                scp = SCPClient(ssh.get_transport())
                scp.get(filename)
            if os.path.isfile(server['file']):
                updateJsonFile(filename)
                print(filename)
            else:
                print('KO')

def updateJsonFile(filename):
        with open('md5.json', 'r') as f:
           data = json.load(f)

       subdata = data['md5s']
       for check in subdata:
           check["filename"] = filename

       with open('md5.json', 'w') as f:
           f.write(json.dumps(data))

filemd5()
</code></pre>

<p>Formatting has not really came out well here but I am nearly sure it is good in my python script. 
What is happening now is that it populates all fields 'filename' with the same file when I am SCP three files from different servers. </p>

<p>Any help would be great. Thanks.</p>

<p>EDIT(updated question as adding to file works but it fills all values with same filename.</p>

<p>Expected result:</p>

<pre><code>{"path":"/home/Document/Python", 
 "md5s":[{"filename":"text1.txt",
"md5":"",
"timestamp":""},
{"filename":"text2.txt",
"md5":"",
"timestamp":""},
{"filename":"text3.txt",
"md5":"",
"timestamp":""}
 ]}
</code></pre>

<p>Actual:</p>

<pre><code> {"path":"/home/Document/Python", 
 "md5s":[{"filename":"text1.txt",
"md5":"",
"timestamp":""},
{"filename":"text1.txt",
"md5":"",
"timestamp":""},
{"filename":"text1.txt",
"md5":"",
"timestamp":""}
 ]}}
</code></pre>

<p>If I have correctly understood, you start with a json file containing a list of references to file, and you want to update the next element of the list.</p>

<p>You could browse the <code>data['md5s']</code> searching for the first element where the  <code>filename</code> field is empty, and add a new dictionnary to the list if all are already completed:</p>

<pre><code>def updateJsonFile(filename):
    jsonFile = open("md5.json", "r")  # load data from disk
    data = json.load(jsonFile)
    jsonFile.close()

    for tmp in data["md5s"]:  # browse the list
        if len(tmp['filename']) == 0:  # found one empty slot, use it and exit looop
            tmp['filename'] = filename
            break
    else:     # no empty slot found: add a new dict
        data["md5s"].append({'md5': '', 'timestamp': '', 'filename': filename})
    jsonFile = open("m.json", "w")  # write the json back to file
    json.dump(data, jsonFile)
    jsonFile.close()
</code></pre>

python json