github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/test/kontrol/scripts/json/clean_json.py (about)

     1  """
     2  Description:
     3      Unescapes the JSON produced by the stateDiff modifier
     4      defined in contracts-bedrock/scripts/Deploy.s.sol
     5      This script is used in ../make-summary-deployment.sh
     6  
     7  Usage:
     8      After producing a state diff JSON with the stateDiff modifier
     9      (e.g. by executing KontrolDeployment::runKontrolDeployment), the JSON is
    10      saved under contracts-bedrock/snapshots/state-diff/Deploy.json. To unescape
    11      it, run: python3 clean_json.py $path_to_state_diff_json
    12      The unescaped JSON will rewrite the input file
    13  """
    14  
    15  import sys
    16  import json
    17  
    18  def clean_json(input_file):
    19      with open(input_file, 'r') as file:
    20          input_string = file.read()
    21  
    22      result = input_string
    23  
    24      # Remove backslashes
    25      result = result.replace('\\', '')
    26  
    27      # Remove " between ] and ,
    28      result = result.replace(']",', '],')
    29  
    30      # Remove " between } and ]
    31      result = result.replace('}"]', '}]')
    32  
    33      # Remove " between [ and {
    34      result = result.replace('["{', '[{')
    35  
    36      # Remove " between } and ,
    37      result = result.replace('}",', '},')
    38  
    39      # Remove " between , and {
    40      result = result.replace(',"{', ',{')
    41  
    42      # Remove " before [{
    43      result = result.replace('"[{', '[{')
    44  
    45      # Remove " after }]
    46      result = result.replace('}]"', '}]')
    47  
    48      with open(input_file, 'w') as file:
    49          file.write(result)
    50  
    51  if __name__ == "__main__":
    52      # Check if a file path is provided as a command line argument
    53      if len(sys.argv) != 2:
    54          print("Usage: clean_json.py <file_path>")
    55          sys.exit(1)
    56  
    57      input_file_path = sys.argv[1]
    58  
    59      clean_json(input_file_path)
    60  
    61      print(f"Operation completed. Result saved to {input_file_path}")