def apply_json_patch(record, ops):
 data = record.copy()
 permissions = {'read': set(), 'write': set()}
 permissions.update(data.pop('__permissions__', {}))
 permissions = {k: {i: i for i in v} for (k, v) in permissions.items()}
 resource = {'data': data, 'permissions': permissions}
 for op in ops:
  if ('path' in op):
   if op['path'].startswith(('/permissions/read/', '/permissions/write/')):
    op['value'] = op['path'].split('/')[(-1)]
 try:
  result = jsonpatch.apply_patch(resource, ops)
 except (jsonpatch.JsonPatchException, jsonpatch.JsonPointerException) as e:
  raise ValueError(e)
 return result

def apply_json_patch ( record , ops ) : data = record . copy ( ) permissions = { 'read' : set ( ) , 'write' : set ( ) } permissions . update ( data . pop ( '__permissions__' , { } ) ) permissions = { k : { i : i for i in v } for ( k , v ) in permissions . items ( ) } resource = { 'data' : data , 'permissions' : permissions } for op in ops : if ( 'path' in op ) : if op [ 'path' ] . startswith ( ( '/permissions/read/' , '/permissions/write/' ) ) : op [ 'value' ] = op [ 'path' ] . split ( '/' ) [ ( - 1 ) ] try : result = jsonpatch . apply_patch ( resource , ops ) except ( jsonpatch . JsonPatchException , jsonpatch . JsonPointerException ) as e : raise ValueError ( e ) return result
apply json patch operations using jsonpatch .