def json_dict_unicode_to_bytes(d, encoding='utf-8', errors='surrogate_or_strict'):
 if isinstance(d, text_type):
  return to_bytes(d, encoding=encoding, errors=errors)
 elif isinstance(d, dict):
  return dict(map(json_dict_unicode_to_bytes, iteritems(d), repeat(encoding), repeat(errors)))
 elif isinstance(d, list):
  return list(map(json_dict_unicode_to_bytes, d, repeat(encoding), repeat(errors)))
 elif isinstance(d, tuple):
  return tuple(map(json_dict_unicode_to_bytes, d, repeat(encoding), repeat(errors)))
 else:
  return d

def json_dict_unicode_to_bytes ( d , encoding = 'utf-8' , errors = 'surrogate_or_strict' ) : if isinstance ( d , text_type ) : return to_bytes ( d , encoding = encoding , errors = errors ) elif isinstance ( d , dict ) : return dict ( map ( json_dict_unicode_to_bytes , iteritems ( d ) , repeat ( encoding ) , repeat ( errors ) ) ) elif isinstance ( d , list ) : return list ( map ( json_dict_unicode_to_bytes , d , repeat ( encoding ) , repeat ( errors ) ) ) elif isinstance ( d , tuple ) : return tuple ( map ( json_dict_unicode_to_bytes , d , repeat ( encoding ) , repeat ( errors ) ) ) else : return d
recursively convert dict keys and values to byte str specialized for json return because this only handles .