def sort_json_policy_dict(policy_dict):
 def value_is_list(my_list):
  checked_list = []
  for item in my_list:
   if isinstance(item, dict):
    checked_list.append(sort_json_policy_dict(item))
   elif isinstance(item, list):
    checked_list.append(value_is_list(item))
   else:
    checked_list.append(item)
  checked_list.sort()
  return checked_list
 ordered_policy_dict = {}
 for (key, value) in policy_dict.items():
  if isinstance(value, dict):
   ordered_policy_dict[key] = sort_json_policy_dict(value)
  elif isinstance(value, list):
   ordered_policy_dict[key] = value_is_list(value)
  else:
   ordered_policy_dict[key] = value
 return ordered_policy_dict

def sort_json_policy_dict ( policy_dict ) : def value_is_list ( my_list ) : checked_list = [ ] for item in my_list : if isinstance ( item , dict ) : checked_list . append ( sort_json_policy_dict ( item ) ) elif isinstance ( item , list ) : checked_list . append ( value_is_list ( item ) ) else : checked_list . append ( item ) checked_list . sort ( ) return checked_list ordered_policy_dict = { } for ( key , value ) in policy_dict . items ( ) : if isinstance ( value , dict ) : ordered_policy_dict [ key ] = sort_json_policy_dict ( value ) elif isinstance ( value , list ) : ordered_policy_dict [ key ] = value_is_list ( value ) else : ordered_policy_dict [ key ] = value return ordered_policy_dict
sort any lists in an iam json policy so that comparison of two policies with identical values but different orders will return true args: policy_dict : dict representing iam json policy .