-1

I need to convert xml to json but not the issue is xml is inside Json element . Is there any way we can do this in AWS lambda function in python ? The XML structure can change also ,I mean elements of xml can change also .

{
    "data": {
        "ID":   "1234",
        "DETAILS":  "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><testCasePayload><testId>1234</testId><status>COMPLETED</status><progressState>ONGOING</progressState><noOfNewSles>5</noOfNewSles><noOfSales>0</noOfSales><noOfAutoSales>0</noOfAutoSales><DealerTypes>WATCHLIST</DealerTypes></testCasePayload>",
    },
    "metadata": {
        "timestamp":    "2021-04-23T11:59:43.259184Z",
        "record-type":  "data",
        "operation":    "load",
        "partition-key-type":   "primary-key",
        "schema-name":  "app",
        "table-name":   "app_event"
    }
}

Expected Output is below In that only DETAILS part which was xml is converted to json

{
  "data":{
    "ID":"1234",
    "DETAILS":{
      "testCasePayload":{
        "testId":"1234",
        "status":"COMPLETED"
        "progressState":"ONGOING",
        "noOfAutoSales":"1",
        "noOfNewSles":"1",
        "noOfSales":"0",
        "DealerTypes":"WATCHLIST",


      }
    }
  },
  "metadata":{
    "timestamp":"2021-04-23T11:59:43.259184Z",
    "record-type":"data",
    "operation":"load",
    "partition-key-type":"primary-key",
    "schema-name":"app",
    "table-name":"app_event"
  }
}
Atharv Thakur
  • 671
  • 3
  • 21
  • 39

1 Answers1

0

Nothing is stopping you from doing this in lambda, the main think is solving the problem with python first.

This question highlights how to turn an xml string into a python dictionary, from there it should be quite straightforward to turn the dictionary into json, and update your original json. Something like:

import json
from xml.etree import cElementTree as ElementTree

original_data = json.loads(original_json)
xml_string = original_data.get('data').get('DETAILS')

# Perform the conversion as per the example in the other question above, you'll have to define the XmlDictConfig class
xml_to_dict = XmlDictConfig(ElementTree.XML(xml_string))

# Update your dictionary
original_data.get('data')['DETAILS'] = xml_to_dict

Main thing to test is if Lambda has access to the xml library, which it should. If not you'll want to read up on lambda layers. Good luck

m.oulmakki
  • 296
  • 1
  • 6