to whoever it might help out
def proto_to_json():
try:
# Run the protoc command
output = subprocess.run(["protoc", "--decode_raw"],
stdin=open("tiktok.txt", "r", encoding="utf-8"),
capture_output=True,
text=True)
except UnicodeDecodeError:
output = subprocess.run(["protoc", "--decode_raw"],
stdin=open("tiktok.txt", "r", errors='ignore'),
capture_output=True,
text=True, stdout=subprocess.PIPE)
output_lines = output.stdout.strip().split("\n")
output_lines = [line.strip() for line in output_lines]
# Define an empty dictionary to store the output
output_dict = {}
# Define a stack to keep track of the nested dictionaries
stack = [output_dict]
# Iterate through the lines and add the key-value pairs to the dictionary
for line in output_lines:
if ": " in line:
key, value = line.split(": ", 1)
stack[-1][key] = value
elif "{" in line:
key = line.replace("{", "")
new_dict = {}
stack[-1][key] = new_dict
stack.append(new_dict)
elif "}" in line:
stack.pop()
# Convert the dictionary to a JSON string
json_output = json.dumps(output_dict, indent=4)
# Write the JSON string to a file
with open("file_name", "w") as f:
f.write(json_output)
return json_output