Skip to main content

Converts XML Responses from AWS API to JSON

Project description

AWS XML to JSON

This repository is a work in progress, contribution is welcome!

This module converts AWS API XML responses to JSON, it matches the output of AWS APIs where Accept: application/json is provided as a header.

Usage

from awsxmltojson import convert_xml_to_dict

convert_xml_to_dict("""
<ListQueuesResponse>
    <ListQueuesResult>
        <QueueUrl>https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue</QueueUrl>
    </ListQueuesResult>
    <ResponseMetadata>
        <RequestId>725275ae-0b9b-4762-b238-436d7c65a1ac</RequestId>
    </ResponseMetadata>
</ListQueuesResponse>
""")
# Outputs...
{
    "ListQueuesResponse": {
        "ListQueuesResult": {
            "queueUrls": [
                "https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue"
            ]
        },
        "ResponseMetadata": {
            "RequestId": "725275ae-0b9b-4762-b238-436d7c65a1ac"
        }
    }
}

Examples

ErrorResponse

<ErrorResponse>
    <Error>
        <Type>Sender</Type>
        <Code>AccessDenied</Code>
        <Message>Access to the resource https://sqs.us-east-1.amazonaws.com/ is denied.</Message>
        <Detail/>
    </Error>
    <RequestId>2d121ac6-aeee-515c-8d04-420e02b34285</RequestId>
</ErrorResponse>
{
    "ErrorResponse": {
        "Error": {
            "Code": "AccessDenied",
            "Message": "Access to the resource https://sqs.us-east-1.amazonaws.com/ is denied.",
            "Type": "Sender"
        },
        "RequestId": "2d121ac6-aeee-515c-8d04-420e02b34285"
    }
}

SQS.ListQueues

<ListQueuesResponse>
    <ListQueuesResult>
        <QueueUrl>https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue</QueueUrl>
    </ListQueuesResult>
    <ResponseMetadata>
        <RequestId>725275ae-0b9b-4762-b238-436d7c65a1ac</RequestId>
    </ResponseMetadata>
</ListQueuesResponse>
{
    "ListQueuesResponse": {
        "ListQueuesResult": {
            "queueUrls": [
                "https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue"
            ]
        },
        "ResponseMetadata": {
            "RequestId": "725275ae-0b9b-4762-b238-436d7c65a1ac"
        }
    }
}

SQS.ReceiveMessage

<ReceiveMessageResponse>
  <ReceiveMessageResult>
    <Message>
      <MessageId>5fea7756-0ea4-451a-a703-a558b933e274</MessageId>
      <ReceiptHandle>MbZj6wDWli+JvwwJaBV+3dcjk2YW2vA3+STFFljTM8tJJg6HRG6PYSasuWXPJB+CwLj1FjgXUv1uSj1gUPAWV66FU/WeR4mq2OKpEGYWbnLmpRCJVAyeMjeU5ZBdtcQ+QEauMZc8ZRv37sIW2iJKq3M9MFx1YvV11A2x/KSbkJ0=</ReceiptHandle>
      <MD5OfBody>fafb00f5732ab283681e124bf8747ed1</MD5OfBody>
      <Body>This is a test message</Body>
      <Attribute>
        <Name>SenderId</Name>
        <Value>195004372649</Value>
      </Attribute>
      <Attribute>
        <Name>SentTimestamp</Name>
        <Value>1238099229000</Value>
      </Attribute>
      <Attribute>
        <Name>ApproximateReceiveCount</Name>
        <Value>5</Value>
      </Attribute>
      <Attribute>
        <Name>ApproximateFirstReceiveTimestamp</Name>
        <Value>1250700979248</Value>
      </Attribute>
    </Message>
  </ReceiveMessageResult>
  <ResponseMetadata>
    <RequestId>b6633655-283d-45b4-aee4-4e84e0ae6afa</RequestId>
  </ResponseMetadata>
</ReceiveMessageResponse>
{
    "ReceiveMessageResponse": {
        "ReceiveMessageResult": {
            "messages": [
                {
                    "MessageId": "5fea7756-0ea4-451a-a703-a558b933e274",
                    "ReceiptHandle": "MbZj6wDWli+JvwwJaBV+3dcjk2YW2vA3+STFFljTM8tJJg6HRG6PYSasuWXPJB+CwLj1FjgXUv1uSj1gUPAWV66FU/WeR4mq2OKpEGYWbnLmpRCJVAyeMjeU5ZBdtcQ+QEauMZc8ZRv37sIW2iJKq3M9MFx1YvV11A2x/KSbkJ0=",
                    "MD5OfBody": "fafb00f5732ab283681e124bf8747ed1",
                    "Body": "This is a test message",
                    "SenderId": "195004372649",
                    "SentTimestamp": "1238099229000",
                    "ApproximateReceiveCount": "5",
                    "ApproximateFirstReceiveTimestamp": "1250700979248"
                }
            ]
        },
        "ResponseMetadata": {
            "RequestId": "b6633655-283d-45b4-aee4-4e84e0ae6afa"
        }
    }
}

SQS.SendMessage

<SendMessageResponse>
    <SendMessageResult>
        <MD5OfMessageBody>fafb00f5732ab283681e124bf8747ed1</MD5OfMessageBody>
        <MD5OfMessageAttributes>3ae8f24a165a8cedc005670c81a27295</MD5OfMessageAttributes>
        <MessageId>5fea7756-0ea4-451a-a703-a558b933e274</MessageId>
    </SendMessageResult>
    <ResponseMetadata>
        <RequestId>27daac76-34dd-47df-bd01-1f6e873584a0</RequestId>
    </ResponseMetadata>
</SendMessageResponse>
{
    "SendMessageResponse": {
        "SendMessageResult": {
            "MD5OfMessageBody": "fafb00f5732ab283681e124bf8747ed1",
            "MD5OfMessageAttributes": "3ae8f24a165a8cedc005670c81a27295",
            "MessageId": "5fea7756-0ea4-451a-a703-a558b933e274"
        },
        "ResponseMetadata": {
            "RequestId": "27daac76-34dd-47df-bd01-1f6e873584a0"
        }
    }
}

Want to add another AWS API?

  1. Download the XML to JSON mapping helper file from the aws javascript sdk
  2. Add it so it's loaded in get_shape.py
  3. Write a couple sample tests use example responses from the AWS documentation to make sure it's working
  4. Run update_readme_with_samples.py to have your samples added to the README

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

awsxmltojson-0.2.3.tar.gz (26.0 kB view details)

Uploaded Source

Built Distribution

awsxmltojson-0.2.3-py3-none-any.whl (25.2 kB view details)

Uploaded Python 3

File details

Details for the file awsxmltojson-0.2.3.tar.gz.

File metadata

  • Download URL: awsxmltojson-0.2.3.tar.gz
  • Upload date:
  • Size: 26.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for awsxmltojson-0.2.3.tar.gz
Algorithm Hash digest
SHA256 4b2fe4dab50607c41f32af57ef780443ae04515993b335c24b245233d3b28f3c
MD5 316bbdf2824dafec2ea60c174dbd3d6e
BLAKE2b-256 c523bc8171baf808cdb3c575735882e9172f941197d81c34c45567dcb0dd1a61

See more details on using hashes here.

File details

Details for the file awsxmltojson-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: awsxmltojson-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 25.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for awsxmltojson-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 47587b112544686699d4f8ffc7f3e5bd0469ad8744a48bcc987786b3752e18ab
MD5 86be0020121539a05f72428066fac86e
BLAKE2b-256 781d61e92c13b525f206dd4f3330895b834a1cc99edee82ce89000f40f5e94cf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page