clean_json.py 1.6 KB
Newer Older
1 2 3
"""
Description:
    Unescapes the JSON produced by the stateDiff modifier
4
    defined in contracts-bedrock/scripts/deploy/Deploy.s.sol
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    This script is used in ../make-summary-deployment.sh

Usage:
    After producing a state diff JSON with the stateDiff modifier
    (e.g. by executing KontrolDeployment::runKontrolDeployment), the JSON is
    saved under contracts-bedrock/snapshots/state-diff/Deploy.json. To unescape
    it, run: python3 clean_json.py $path_to_state_diff_json
    The unescaped JSON will rewrite the input file
"""

import sys

def clean_json(input_file):
    with open(input_file, 'r') as file:
        input_string = file.read()

    result = input_string

    # Remove backslashes
    result = result.replace('\\', '')

    # Remove " between ] and ,
    result = result.replace(']",', '],')

    # Remove " between } and ]
    result = result.replace('}"]', '}]')

    # Remove " between [ and {
    result = result.replace('["{', '[{')

    # Remove " between } and ,
    result = result.replace('}",', '},')

    # Remove " between , and {
    result = result.replace(',"{', ',{')

    # Remove " before [{
    result = result.replace('"[{', '[{')

    # Remove " after }]
    result = result.replace('}]"', '}]')

    with open(input_file, 'w') as file:
        file.write(result)

if __name__ == "__main__":
    # Check if a file path is provided as a command line argument
    if len(sys.argv) != 2:
        print("Usage: clean_json.py <file_path>")
        sys.exit(1)

    input_file_path = sys.argv[1]

    clean_json(input_file_path)

    print(f"Operation completed. Result saved to {input_file_path}")