-
Notifications
You must be signed in to change notification settings - Fork 0
/
bodsutils.py
58 lines (52 loc) · 1.8 KB
/
bodsutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import uuid
import jsonpatch
from jsonschema import validate
from jsonpath_ng import jsonpath, parse
__all__ = ['find_matching_paths',
'json_paths_to_json_patch_paths',
'patch_replace_from_paths_values',
'patch_delete_from_paths',
'replace_uuids']
def find_matching_paths(json, jsonpexpr = '$..id'):
jsonpath_expr = parse(jsonpexpr)
return [str(match.full_path) for match in jsonpath_expr.find(json)]
def json_paths_to_json_patch_paths(paths):
patch_paths = []
for path in paths:
p = str.replace(path, ".", "/")
p = str.replace(p, "[", "")
p = str.replace(p, "]", "")
patch_paths.append("/" + p)
return patch_paths
def patch_replace_from_paths_values(paths, values):
patches = []
for p, v in zip(paths, values):
patch = {'op': 'replace',
'path': p,
'value': v}
patches.append(patch)
return jsonpatch.JsonPatch(patches)
def patch_delete_from_paths(paths):
patches = []
for p in paths:
patch = {'op': 'remove',
'path': p
}
patches.append(patch)
return jsonpatch.JsonPatch(patches)
def replace_uuids(json):
"""
Replace the uuids in each 'id' element.
Assumes a Beneficial Ownership Data Standard object,
and so exlcudes id fields within an 'identifiers' object.
"""
# find matching JSON Paths
matches = find_matching_paths(json)
matches = [match for match in matches if 'identifier' not in match]
# convert paths to JSON Patch paths
paths = json_paths_to_json_patch_paths(matches)
# create a JSON Patch object
uuids = [str(uuid.uuid4()) for _ in range(len(paths))]
patch = patch_replace_from_paths_values(paths, uuids)
result = patch.apply(json)
return result