def convertFromXmlToJson()

in lambdas/on_file_receive/app.py [0:0]


def convertFromXmlToJson(bucket, key):
    fileContent = s3.Object(bucket, key).get()['Body'].read().decode('utf-8')
    try:
        jsonContent = xmltodict.parse(fileContent)
        jsonContent.update({"filename": key})
        return json.dumps(jsonContent)
    except Exception as e: 
        logger.error({
            "message": "Error converting file {} on bucket{}".format(key, bucket),
            "file": key,
            }, 
            exc_info=True)
        # in case of errors, we send the content a little bit more strucuted 
        # in order to help on further steps
        payload = json.dumps({
            "filename": key,
            "exception": str(e),
            "fileContent": str(fileContent)
        })
        generateEvent(payload, status="file-converted-error")
        return None