Created
May 15, 2020 15:11
-
-
Save realpacific/c00e9b9c823a10c861e1dbb188b9fcdd to your computer and use it in GitHub Desktop.
Convert Typescript object to json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import sys | |
import os | |
filename=sys.argv[1] | |
newline = "" | |
with open(filename, 'r') as file: | |
for line in file.readlines(): | |
line= line.replace("'", "\"") | |
found = re.search("([a-zA-Z]+):", line) | |
if found is not None: | |
span = found.span() | |
hit = found.string[span[0]: span[1] - 1] | |
newline += re.sub(hit[0:], f"\"{hit[0:]}\"", line) | |
else: | |
newline += line | |
newline = re.sub(",[\n\r][\s]+}", "}", newline) | |
newline = re.sub("},[\n\r][\s]+\]", "}]", newline) | |
with open(filename, 'w') as file: | |
file.write(newline) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment