Search code examples
pythonjsonpython-3.xmsgpack

Parse messagePack to Json with python


I have that buffer

87 a1 41 a4 31 32 33 34 a1 42 a4 61 62 63 64 a1 43 a1 61 a1 44 a8 31 31 31 31 31 31 31 31 a1 45 a8 75 73 65 72 6e 61 6d 65 a1 46 a4 6e 61 6d 65 a1 47 a7 6e 61 6d 65 5f 6e 61

and I want to parse to this json

{
    "A": "1234",
    "B": "abcd",
    "C": "a",
    "D": "11111111",
    "E": "username",
    "F": "name",
    "G": "name_na"
}

Like Cybershef

How can I do that with Python please?


Solution

  • Install msgpack: pip3 install msgpack

    And run the following code:

    >>> import msgpack
    >>> hex_str = "87 a1 41 a4 31 32 33 34 a1 42 a4 61 62 63 64 a1 43 a1 61 a1 44 a8 31 31 31 31 31 31 31 31 a1 45 a8 75 73 65 72 6e 61 6d 65 a1 46 a4 6e 61 6d 65 a1 47 a7 6e 61 6d 65 5f 6e 61"
    >>> bytes_array = bytes.fromhex(hex_str)
    >>> msgpack.loads(bytes_array)
    {'A': '1234', 'B': 'abcd', 'C': 'a', 'D': '11111111', 'E': 'username', 'F': 'name', 'G': 'name_na'}