Skip to content Skip to sidebar Skip to footer

Decode Text Response From Api In Python 3.6

I am trying to extract data from mailchimp export api, which returns responses based on the following specifications: Returns: Parameter - text Description: a plain te

Solution 1:

You can get all the data from the MailChimp export api using a simple approach. Please note that I am using f-strings, only available in Python 3.6+.

import requests
import json


apikey = '<your-api-key>'id = "<list-id>"

URL = f"https://us10.api.mailchimp.com/export/1.0/campaignSubscriberActivity/?apikey={apikey}&id={id}"

json_data = [json.loads(s) for s in requests.get(URL).text.strip().split("\n")]
print(json_data[0]['<some-subscriber-email>'][0]['action'])

Solution 2:

Provided that the text response isn't insanely badly formed json, you can use the json library. In particular, the loads() function.

importjsonjson_response= json.loads(response)

loads() loads JSON into a python dict from a string.

EDIT: The Mailchimp API states that each JSON object is separated by a newline character. We can create a list of dicts with the following code:

# get response fromGET request and load as a string
import json
json_resp = [json.loads(line) for line in response.split('\n')]

Post a Comment for "Decode Text Response From Api In Python 3.6"