-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
66 lines (54 loc) · 2.38 KB
/
graph.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
# import json
# import uuid
# # Read JSON data from a file
# with open('/Users/BB/CashZar/datagraph/schema.json', 'r') as file:
# data = json.load(file)
# # Extract the data from the "graph" key
# graph_data = data['graph']
# # Prepare the output dictionary
# output_data = {}
# # Process each key in the "graph" data
# for key, value in graph_data.items():
# # Check if the current key should be processed
# # You can add more conditions here if needed
# if isinstance(value, list): # Assuming we process if the value is a list
# # Generate a unique identifier
# unique_id = str(uuid.uuid4())
# # Create a new entry in the output data with a new format
# output_data[unique_id] = {
# "name": key,
# "description": "this is description",
# "relationships": value
# }
# # Output the JSON to a file or print it
# print(json.dumps(output_data, indent=4))
# # Optionally, write the output to a file
# with open('/Users/BB/CashZar/datagraph/output.json', 'w') as file:
# json.dump(output_data, file, indent=4)
import json
# Sample data read from a file, assuming it's named 'data.json'
with open("/Users/BB/CashZar/datagraph/output.json", "r") as file:
data = json.load(file)
# Create a lookup table for node names to IDs
name_to_id = {}
for node_id, node_info in data.items():
name_to_id[node_info['name']] = node_id
# Replace names in relationships with IDs
for node_info in data.values():
if 'relationships' in node_info:
for relationship in node_info['relationships']:
# Process every key in the relationship that contains a list
for key, names in relationship.items():
if isinstance(names, list): # Ensure it's a list before processing
updated_items = []
for name in names:
# Map the name to its ID using the lookup table
if name in name_to_id:
updated_items.append(name_to_id[name])
else:
updated_items.append(name) # Keep the original if no ID is found
relationship[key] = updated_items
# Output the updated data to a file or print it
print(json.dumps(data, indent=4))
with open("/Users/BB/CashZar/datagraph/output1.json", "w") as file:
json.dump(data, file, indent=4)