From 573b4cbea0176e0787d897658b50875f843270f8 Mon Sep 17 00:00:00 2001 From: adasium Date: Wed, 6 Mar 2024 00:00:12 +0100 Subject: [PATCH] sort keys --- bin/chom_json2csv.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bin/chom_json2csv.py b/bin/chom_json2csv.py index 7ed4ab2..a13f9fe 100755 --- a/bin/chom_json2csv.py +++ b/bin/chom_json2csv.py @@ -2,6 +2,7 @@ from __future__ import annotations import csv +import collections import json import sys import argparse @@ -122,10 +123,10 @@ def main(argv: Optional[Sequence[str]] = None) -> int: args = parser.parse_args() if args.debug: print(args.columns) - data = json.load(sys.stdin) - json_fields = set() + data = json.load(sys.stdin, object_pairs_hook=collections.OrderedDict) + json_fields = [] for doc in data: - json_fields |= set(doc) + json_fields = list(dict.fromkeys(json_fields + list(doc.keys()))) csv_columns = args.columns or json_fields print(','.join(csv_columns))