1. import json
  2. import argparse
  3. def format_api_output(data, overrides=None):
  4. """
  5. Formats API output with manual overrides for internal tooling.
  6. Args:
  7. data (dict): The raw API response data.
  8. overrides (dict, optional): A dictionary of key-value pairs to override
  9. in the output. Defaults to None.
  10. Returns:
  11. str: Formatted JSON string.
  12. """
  13. formatted_data = {}
  14. for key, value in data.items():
  15. if key in overrides:
  16. formatted_data[key] = overrides[key] # Apply override if available
  17. else:
  18. formatted_data[key] = value
  19. return json.dumps(formatted_data, indent=4)
  20. if __name__ == '__main__':
  21. parser = argparse.ArgumentParser(description='Format API output.')
  22. parser.add_argument('--input_data', type=str, required=True, help='JSON input data')
  23. parser.add_argument('--overrides', type=str, help='JSON override data')
  24. args = parser.parse_args()
  25. try:
  26. data = json.loads(args.input_data)
  27. except json.JSONDecodeError as e:
  28. print(f"Error decoding input JSON: {e}")
  29. exit(1)
  30. overrides = None
  31. if args.overrides:
  32. try:
  33. overrides = json.loads(args.overrides)
  34. except json.JSONDecodeError as e:
  35. print(f"Error decoding overrides JSON: {e}")
  36. exit(1)
  37. formatted_output = format_api_output(data, overrides)
  38. print(formatted_output)

Add your comment