-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_replace.py
78 lines (61 loc) · 2.7 KB
/
02_replace.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import json
def find_and_replace(node):
"""
Read JSON file, then iterates over the data to find and replace nodes based
on the specified conditions. It handles multiple levels of nesting, keeps the
'value' and 'isreplacecandidate' fields in the replaced candidate and updates the
'isreplaced' field to True. It deletes the 'isoriginal' field in the replaced node.
This function is useful for manipulating hierarchical JSON data, particularly
to replace certain nodes based on specific criteria. The function is a recursive
function that operates on a node of a JSON structure.
NEXT STEPS:
- count iterations and stop infinite replications
(A) IMPORTANT: The replace candidate in curly brackets has to be the highest indent node.
(A.1) This example will work:
[rel_key_I]
{static_harmony}
cadence
(A.2) This example will work:
[rel_key_I]
{static_harmony}
cadence
(A.3) This example will NOT work:
[rel_key_I]
{static_harmony}
cadence
Parameters:
node (dict): This is a dictionary representing a node in the JSON structure. Each
node is expected to have the following keys: 'description', 'isreplacecandidate',
'isreplaced', and optionally 'children' if the node has child nodes.
Returns:
The function does not explicitly return a value. Instead, it modifies the input
JSON structure in-place. Specifically, it iterates over the data to find and replace
nodes based on the specified conditions. If a node is marked as a replace candidate
and has not been replaced yet, it replaces the 'children' of the node with those of
the original node with the same 'description', and updates the 'isreplaced' field
to True.
"""
if node['isreplacecandidate'] and not node['isreplaced']:
for original_node in original_nodes:
if original_node['description'] == node['description']:
node['children'] = original_node['children']
node['isreplaced'] = True
for child in node.get('children', []):
find_and_replace(child)
# MODE A: 02_replicate --> 03_replace
# input_file = "json\\output_replicate-6.json"
# output_file = "json\\output_replaced.json"
# MODE B: 03_replace --> 02_replicate
input_file = "json\\output.json"
output_file = "json\\output_replaced.json"
# Load the data
with open(input_file, 'r') as f:
data = json.load(f)
# Find the original nodes
original_nodes = [node for node in data if node['isoriginal']]
# Perform the replacement
for node in data:
find_and_replace(node)
# Save the result
with open(output_file, 'w') as f:
json.dump(data, f, indent=4)