import dicttoxml
json_req_body = {
"req": {
"SessionKey": "aad3584e-ce40-4937-9eae-5084ab693986",
"ObjectId": "1f79ed70-77c4-ec11-997e-281878c34860",
"PhoneNumber": {
"EntryId": 0,
"CountryCode": "",
"PhoneNumberType": "Home",
"_PhoneNumber": "7073861807",
"_IsPrimary": "true",
"OptOut": "false"
}
}
}
json_to_xml_data = dicttoxml.dicttoxml(json_req_body, attr_type=False,root=False)
#Byte to String
json_to_xml_data = json_to_xml_data.decode("utf-8")
print(json_to_xml_data)
current output:
<req>
<SessionKey>aad3584e-ce40-4937-9eae-5084ab693986</SessionKey>
<ObjectId>1f79ed70-77c4-ec11-997e-281878c34860</ObjectId>
<PhoneNumber>
<EntryId>0</EntryId>
<CountryCode></CountryCode>
<PhoneNumberType>Home</PhoneNumberType>
<_PhoneNumber>7073861807</_PhoneNumber>
<_IsPrimary>true</_IsPrimary>
<OptOut>false</OptOut>
</PhoneNumber>
</req>
I am using dicttoxml package to convert JSON to XML, conversation is happening well. but I have a scenario where JSON keys start with _ . in that scenario need JSON key and values have to be part of the XML parent tag as follows.
expected output;
<req>
<SessionKey>aad3584e-ce40-4937-9eae-5084ab693986</SessionKey>
<ObjectId>1f79ed70-77c4-ec11-997e-281878c34860</ObjectId>
<PhoneNumber PhoneNumber='7073861807' IsPrimary='true'>
<EntryId>0</EntryId>
<CountryCode></CountryCode>
<PhoneNumberType>Home</PhoneNumberType>
<OptOut>false</OptOut>
</PhoneNumber>
</req>
is there a way to achieve this using dicttoxml package, or are there any other packages that support these scenarios?