From 123cebad8e4baa2f6a4b7327b25bf3e6a28f1627 Mon Sep 17 00:00:00 2001 From: Cyber MacGeddon Date: Wed, 3 Sep 2025 23:35:27 +0100 Subject: [PATCH] Preserve original order --- .../trustgraph/cli/invoke_objects_query.py | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/trustgraph-cli/trustgraph/cli/invoke_objects_query.py b/trustgraph-cli/trustgraph/cli/invoke_objects_query.py index 7a622827..50c4e8c2 100644 --- a/trustgraph-cli/trustgraph/cli/invoke_objects_query.py +++ b/trustgraph-cli/trustgraph/cli/invoke_objects_query.py @@ -42,11 +42,15 @@ def format_table_data(rows, table_name, output_format): return json.dumps({table_name: rows}, indent=2) elif output_format == 'csv': - # Get all unique field names - fieldnames = set() + # Get field names in order from first row, then add any missing ones + fieldnames = list(rows[0].keys()) if rows else [] + # Add any additional fields from other rows that might be missing + all_fields = set(fieldnames) for row in rows: - fieldnames.update(row.keys()) - fieldnames = sorted(fieldnames) + for field in row.keys(): + if field not in all_fields: + fieldnames.append(field) + all_fields.add(field) # Create CSV string output = io.StringIO() @@ -56,11 +60,15 @@ def format_table_data(rows, table_name, output_format): return output.getvalue().rstrip() elif output_format == 'table': - # Get all unique field names - fieldnames = set() + # Get field names in order from first row, then add any missing ones + fieldnames = list(rows[0].keys()) if rows else [] + # Add any additional fields from other rows that might be missing + all_fields = set(fieldnames) for row in rows: - fieldnames.update(row.keys()) - fieldnames = sorted(fieldnames) + for field in row.keys(): + if field not in all_fields: + fieldnames.append(field) + all_fields.add(field) # Create table data table_data = []