#!/bin/bash

# Usage: bash flatten_preds.sh input.json output.json
# input.json: a JSON file containing a dict of dicts
# output.json: the output JSON file containing a list of dicts

INPUT_FILE="$1"
OUTPUT_FILE="$2"

python3 -c "
import sys, json
with open(sys.argv[1]) as f:
    d = json.load(f)
if isinstance(d, dict):
    # If it's a dict of dicts, flatten to list of dicts
    out = list(d.values())
else:
    out = d
with open(sys.argv[2], 'w') as f:
    json.dump(out, f, indent=2)
" "$INPUT_FILE" "$OUTPUT_FILE"
