def convert_pkt_to_json(pkg):
results = defaultdict(dict)<EOL>try:<EOL><INDENT>for index in range(<NUM_LIT:0>, len(pkg)):<EOL><INDENT>layer = pkg[index]<EOL>layer_tmp_name = str(layer.__dict__["<STR_LIT>"][<NUM_LIT:0>])<EOL>layer_start_pos = layer_tmp_name.rfind("<STR_LIT:.>") + <NUM_LIT:1><EOL>layer_name = layer_tmp_name[layer_start_pos:-<NUM_LIT:2>].lower()<EOL>tmp_t = {}<EOL>for default_x, y in layer.__dict__["<STR_LIT>"].items():<EOL><INDENT>x = "<STR_LIT>".format(default_x)<EOL>if DEBUG_PACKETS:<EOL><INDENT>log.info("<STR_LIT>".format(x, y))<EOL><DEDENT>try:<EOL><INDENT>tmp_t["<STR_LIT>".format(default_x)] = y.hex()<EOL><DEDENT>except Exception:<EOL><INDENT>if y and not isinstance(y, (str,<EOL>int,<EOL>int,<EOL>float,<EOL>list,<EOL>dict)):<EOL><INDENT>if x in tmp_t:<EOL><INDENT>tmp_t[x].update(convert_pkt_to_json(y))<EOL><DEDENT>else:<EOL><INDENT>tmp_t[x] = y<EOL><DEDENT><DEDENT>else:<EOL><INDENT>tmp_t[x] = y<EOL><DEDENT><DEDENT><DEDENT>results[layer_name] = tmp_t<EOL>try:<EOL><INDENT>tmp_t = {}<EOL>for fields_x, y in layer.__dict__["<STR_LIT>"].items():<EOL><INDENT>if DEBUG_PACKETS:<EOL><INDENT>log.info("<STR_LIT>".format(x, y))<EOL><DEDENT>if fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>tmp_t["<STR_LIT>"] = json.loads(<EOL>convert_pkt_to_json(y))<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>tmp_t["<STR_LIT>"] = json.loads(<EOL>convert_pkt_to_json(y))<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>tmp_t["<STR_LIT>"] = json.loads(<EOL>convert_pkt_to_json(y))<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>tmp_t["<STR_LIT>"] = json.loads(<EOL>convert_pkt_to_json(y))<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>"""<STR_LIT>"""<EOL>tmp_t["<STR_LIT>"] = str(y)<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>tmp_t[x] = y<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>tmp_t[x] = y<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>tmp_t[x] = y<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT:id>":<EOL><INDENT>if y:<EOL><INDENT>tmp_t[x] = y<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>tmp_t[x] = y<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>tmp_t[x] = y<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>tmp_t[x] = y<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>tmp_t[x] = y<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>tmp_t[x] = y<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>tmp_t[x] = y<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>cur_d = {}<EOL>try:<EOL><INDENT>test = dict(y)<EOL>if "<STR_LIT>" in test:<EOL><INDENT>cur_d["<STR_LIT>"] = test["<STR_LIT>"]<EOL><DEDENT>if "<STR_LIT>" in test:<EOL><INDENT>cur_d["<STR_LIT>"] = test["<STR_LIT>"]<EOL><DEDENT>if "<STR_LIT>" in test:<EOL><INDENT>cur_d["<STR_LIT>"] = test["<STR_LIT>"]<EOL><DEDENT>if "<STR_LIT>" in test:<EOL><INDENT>cur_d["<STR_LIT>"] = test["<STR_LIT>"]<EOL><DEDENT>if "<STR_LIT>" in test:<EOL><INDENT>cur_d["<STR_LIT>"] =test["<STR_LIT>"].decode("<STR_LIT:utf-8>")<EOL><DEDENT>if "<STR_LIT>" in test:<EOL><INDENT>cur_d["<STR_LIT>"] = test["<STR_LIT>"]<EOL><DEDENT>if "<STR_LIT>" in test:<EOL><INDENT>if test["<STR_LIT>"]:<EOL><INDENT>cur_d["<STR_LIT>"] =test["<STR_LIT>"][<NUM_LIT:0>]<EOL><DEDENT><DEDENT>if "<STR_LIT>" in test:<EOL><INDENT>cur_d["<STR_LIT>"] = test["<STR_LIT>"]<EOL><DEDENT>if "<STR_LIT>" in test:<EOL><INDENT>cur_d["<STR_LIT>"] =test["<STR_LIT>"]<EOL><DEDENT>if "<STR_LIT>" in test:<EOL><INDENT>cur_d["<STR_LIT>"] = test["<STR_LIT>"]<EOL><DEDENT>if "<STR_LIT>" in test:<EOL><INDENT>cur_d["<STR_LIT>"] = test["<STR_LIT>"]<EOL><DEDENT><DEDENT>except Exception as exct:<EOL><INDENT>log.error(("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>.format(x,<EOL>y,<EOL>exct))<EOL>cur_d = str(y)<EOL><DEDENT>tmp_t["<STR_LIT>".format(fields_x)] = cur_d<EOL><DEDENT><DEDENT>elif fields_x == "<STR_LIT>":<EOL><INDENT>if y:<EOL><INDENT>cur_d = {}<EOL>try:<EOL><INDENT>for f in y:<EOL><INDENT>cur_f = "<STR_LIT>".format(fields_x,<EOL>f)<EOL>try:<EOL><INDENT>cur_d[cur_f] = y.decode("<STR_LIT:utf-8>")<EOL><DEDENT>except Exception:<EOL><INDENT>cur_d["<STR_LIT>" + cur_f] = y[f].hex()<EOL><DEDENT><DEDENT><DEDENT>except Exception as exct:<EOL><INDENT>log.error(("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>.format(x,<EOL>y,<EOL>exct))<EOL>cur_d = y<EOL><DEDENT>tmp_t["<STR_LIT>".format(fields_x)] = cur_d<EOL><DEDENT><DEDENT>else:<EOL><INDENT>x = "<STR_LIT:{}>".format(fields_x)<EOL>try:<EOL><INDENT>hex_key = "<STR_LIT>".format(fields_x)<EOL>if fields_x == "<STR_LIT>":<EOL><INDENT>try:<EOL><INDENT>tmp_t["<STR_LIT>"] = y.decode("<STR_LIT:utf-8>")<EOL><DEDENT>except Exception:<EOL><INDENT>tmp_t[hex_key] = y.hex()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>tmp_t[hex_key] = y.hex()<EOL><DEDENT><DEDENT>except Exception:<EOL><INDENT>if y and not isinstance(y, (str,<EOL>int,<EOL>int,<EOL>float,<EOL>list,<EOL>dict)):<EOL><INDENT>if x in tmp_t:<EOL><INDENT>tmp_t[x].update(convert_pkt_to_json(y))<EOL><DEDENT>else:<EOL><INDENT>tmp_t[x] = y<EOL><DEDENT><DEDENT>else:<EOL><INDENT>tmp_t[x] = y<EOL><DEDENT><DEDENT><DEDENT><DEDENT>results[layer_name] = tmp_t<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>except Exception:<EOL><INDENT>pass<EOL><DEDENT>if "<STR_LIT>" in results:<EOL><INDENT>try:<EOL><INDENT>if "<STR_LIT>" in results["<STR_LIT>"]:<EOL><INDENT>results["<STR_LIT>"]["<STR_LIT>"] =results["<STR_LIT>"]["<STR_LIT>"].encode("<STR_LIT:utf-8>").hex()<EOL><DEDENT><DEDENT>except Exception:<EOL><INDENT>log.error(("<STR_LIT>")<EOL>.format(results["<STR_LIT>"]))<EOL><DEDENT><DEDENT>if "<STR_LIT>" in results:<EOL><INDENT>try:<EOL><INDENT>if "<STR_LIT>" in results["<STR_LIT>"]:<EOL><INDENT>results["<STR_LIT>"]["<STR_LIT>"] =results["<STR_LIT>"]["<STR_LIT>"].encode("<STR_LIT:utf-8>").hex()<EOL><DEDENT><DEDENT>except Exception:<EOL><INDENT>log.error(("<STR_LIT>")<EOL>.format(results["<STR_LIT>"]))<EOL><DEDENT><DEDENT>if DEBUG_PACKETS:<EOL><INDENT>log.debug("<STR_LIT>")<EOL>log.debug("<STR_LIT>")<EOL>log.debug(results)<EOL>log.debug("<STR_LIT>")<EOL>log.debug(ppj(results))<EOL>log.debug("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>log.info(ppj(results))<EOL><DEDENT>return results<EOL>
convert_pkt_to_json
    Inspired by:
    https://gist.githubusercontent.com/cr0hn/1b0c2e672cd0721d3a07/raw/9144676ceb12dbd545e6dce366822bbedde8de2c/pkg_to_json.py
    This function convert a Scapy packet to JSON

    :param pkg: A kamene package
    :type pkg: objects

    :return: A JSON data
    :rtype: dict()