1+ import json
2+ import os
3+ import argparse
4+
5+
6+ def traverse_and_replace (obj , path , new_field_name ):
7+ if len (path ) == 1 :
8+ if path [0 ] in obj :
9+ keys = list (obj .keys ())
10+ index = keys .index (path [0 ])
11+ obj [new_field_name ] = obj .pop (path [0 ])
12+ keys = list (obj .keys ()) # Update keys after replacing the key
13+ reordered = {k : obj [k ] for k in keys [:index ] + [new_field_name ] + keys [index :]}
14+ obj .clear ()
15+ obj .update (reordered )
16+ else :
17+ if path [0 ] in obj and isinstance (obj [path [0 ]], dict ):
18+ traverse_and_replace (obj [path [0 ]], path [1 :], new_field_name )
19+ elif path [0 ] in obj and isinstance (obj [path [0 ]], list ):
20+ if len (path ) > 1 and isinstance (path [1 ], int ) and path [1 ] < len (obj [path [0 ]]):
21+ traverse_and_replace (obj [path [0 ]][path [1 ]], path [2 :], new_field_name )
22+
23+ def replace_field_names (input_file_path , output_file_path , changes ):
24+ """
25+ Replace field names in the JSON file based on the given changes.
26+
27+ :param input_file: Path to the input JSON file.
28+ :param output_file: Path to the output JSON file.
29+ :param changes: List of dictionaries containing paths and new field names.
30+ """
31+
32+ with open (input_file_path , 'r' , encoding = 'utf-8' ) as file :
33+ data = json .load (file )
34+
35+ for change in changes :
36+ key_path = change ['path' ]
37+ new_field_name = change ['new_name' ]
38+ traverse_and_replace (data , key_path , new_field_name )
39+
40+ with open (output_file_path , 'w' , encoding = 'utf-8' ) as file :
41+ json .dump (data , file , indent = 4 )
42+
43+ def convert_input_paths_to_changes (input_paths ):
44+ """
45+ Convert input paths to a list of changes with parsed paths and new field names.
46+
47+ :param input_paths: List of dictionaries containing string paths and new field names.
48+ :return: List of dictionaries with parsed paths and new field names.
49+ """
50+ changes = []
51+ for item in input_paths :
52+ path_str = item ["path" ]
53+ path_parts = []
54+ for part in path_str .split ('.' ):
55+ if '[' in part and ']' in part :
56+ field , index = part .split ('[' )
57+ path_parts .append (field )
58+ path_parts .append (int (index .strip (']' )))
59+ else :
60+ path_parts .append (part )
61+ changes .append ({
62+ "path" : path_parts ,
63+ "new_name" : item ["new_field_name" ]
64+ })
65+ return changes
66+
67+
68+ if __name__ == "__main__" :
69+ parser = argparse .ArgumentParser (description = "Replace field names in a JSON file." )
70+ parser .add_argument ("-i" , "--input" , help = "Path to the input JSON file." )
71+ parser .add_argument ("-o" , "--output" , help = "Path to the output JSON file." )
72+ args = parser .parse_args ()
73+
74+ input_file = args .input if args .input else "cybersource-rest-spec.json"
75+
76+ output_file = args .output if args .output else "cybersource-rest-spec-output.json"
77+
78+
79+ inputPaths = [
80+ {
81+ "path" : "paths./ipl/v2/payment-links.get.responses.200.schema.properties.links" ,
82+ "new_field_name" : "sdkLinks"
83+ }
84+
85+ # example of how to add more paths
86+ # ,{
87+ # "path": "paths./pts/v2/payments.post.parameters[0].schema.properties.clientReferenceInformation.properties.code",
88+ # "new_field_name": "sdkCode"
89+ # }
90+ ]
91+
92+ # Convert inputPaths to changes
93+ changes = convert_input_paths_to_changes (inputPaths )
94+
95+ script_dir = os .path .dirname (os .path .abspath (__file__ ))
96+ input_file_path = os .path .join (script_dir , input_file )
97+ output_file_path = os .path .join (script_dir , output_file )
98+ replace_field_names (input_file_path , output_file_path , changes )
0 commit comments