import json
import argparse
def format_api_output(data, overrides=None):
"""
Formats API output with manual overrides for internal tooling.
Args:
data (dict): The raw API response data.
overrides (dict, optional): A dictionary of key-value pairs to override
in the output. Defaults to None.
Returns:
str: Formatted JSON string.
"""
formatted_data = {}
for key, value in data.items():
if key in overrides:
formatted_data[key] = overrides[key] # Apply override if available
else:
formatted_data[key] = value
return json.dumps(formatted_data, indent=4)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Format API output.')
parser.add_argument('--input_data', type=str, required=True, help='JSON input data')
parser.add_argument('--overrides', type=str, help='JSON override data')
args = parser.parse_args()
try:
data = json.loads(args.input_data)
except json.JSONDecodeError as e:
print(f"Error decoding input JSON: {e}")
exit(1)
overrides = None
if args.overrides:
try:
overrides = json.loads(args.overrides)
except json.JSONDecodeError as e:
print(f"Error decoding overrides JSON: {e}")
exit(1)
formatted_output = format_api_output(data, overrides)
print(formatted_output)
Add your comment