Home > Blockchain >  Convert the output of protoc --decode_raw into json
Convert the output of protoc --decode_raw into json

Time:01-27

I'm trying to convert the message of a protobuf blob into a json, without the corresponding schema. This is the code I'm using but it doesn't get nested objects. Maybe there is a way to convert blobs without the schema? I just need a json. The variable names don't matter to me.

message_dict = {}
for line in result.stdout.split("\n"):
    if not line:
        continue
    parts = line.split(": ")
    field_number = parts[0]
    value = parts[1] if len(parts) > 1 else None
    message_dict[field_number] = value

CodePudding user response:

You can write the .proto schema yourself, based on information you learn from --decode_raw.

After that it is easy to convert to JSON using the function google.protobuf.json_format.MessageToJson.

CodePudding user response:

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
  • Related