-1

I have this kind of Json tree for folder structure. Is there any way to compare it with same kind of Json tree to get differences (file missing or different file properties (date,crc,..)) and return this as a list with names of different/missing files.

{
      "testfolder": {
        "children": {
          "content.json": {
            "last_modified_timestamp": 1485902084.0222416, 
            "created_timestamp": 1485193414.5027652, 
            "crc": "7c71cf7ff765ddd78fffcac2eed56ae2", 
            "type": "file", 
            "size": 961
          }, 
          "config.json": {
            "last_modified_timestamp": 1484831126.4821935, 
            "created_timestamp": 1484830625.6165457, 
            "crc": "bff5d42e18df483841aa10df8b38cdd4", 
            "type": "file", 
            "size": 132
          }
        }
      },  
      "__init__.py": {
        "last_modified_timestamp": 1481651800.7150106, 
        "created_timestamp": 1481651800.7150106, 
        "crc": "d41d8cd98f00b204e9800998ecf8427e", 
        "type": "file", 
        "size": 0
      }, 
      "test.json": {
        "last_modified_timestamp": 1486126931.2528062, 
        "created_timestamp": 1486126732.7074502, 
        "crc": "8a30d9b3834ef46ad3b996edb06c72bf", 
        "type": "file", 
        "size": 1675
      }, 
      "test": {
        "children": {
          "test.txt.txt": {
            "last_modified_timestamp": 1486126927.9266162, 
            "created_timestamp": 1486126865.9750726, 
            "crc": "b5301fdbf2ba41520b255a651c7017b1", 
            "type": "file", 
            "size": 5
          }
        }
      }
    }

Thank you for help!

Slajc
  • 19
  • 3
  • So what have you tried so far? – Christian W. Feb 22 '17 at 08:35
  • 1
    load both in dictionaries and then use the numerous answers on comparing dicts, like that one: http://stackoverflow.com/questions/4527942/comparing-two-dictionaries-in-python – Jean-François Fabre Feb 22 '17 at 08:37
  • I have tried with recursion that returns list of files with full paths and than just difference between two lists but that solved only missing files not different files. – Slajc Feb 22 '17 at 08:43

1 Answers1

0
def jsondiff(local,online,path='',todo=[]):
  for key in local.keys():
      if not online.has_key(key):
          if local[key].has_key('children'):
              todo = todo + json_path_print(local[key]["children"],path+key+"/")
          else:
              todo.append(path+key)
      else:
          if local[key].has_key('children'):
              todo=todo+jsondiff(local[key]["children"],online[key]["children"],path+key+"/")
          else:
              if local[key]["last_modified_timestamp"]>online[key]["last_modified_timestamp"]:
                  todo.append(path + key)
  return todo

Solved it if anyone need solution

Slajc
  • 19
  • 3