import json


def parse_response(msg):
    """
    Parses CPqD ASR messages and returns a string corresponding to the
    response type and two dicts, the first one corresponding to the
    header, and the second one to the JSON body.
    """
    msg = msg.data
    msg = msg.replace(b"\r", b"")
    msg = msg.split(b"\n\n")
    header = msg[0].decode().split("\n")
    h = {}
    r = header[0].split()[-1]
    for l in header[1:]:
        split = [x.strip() for x in l.split(":")]
        key = split[0]
        val = ":".join(split[1:])
        h[key] = val

    body = b"\n\n".join(msg[1:])
    if body:
        b = json.loads(body.decode())
    else:
        b = {}
    return r, h, b