Decode text response from API in Python 3.6

被刻印的时光 ゝ 提交于 2019-12-24 14:14:51

问题


I am trying to extract data from mailchimp export api, which returns responses based on the following specifications:

    Returns:

Parameter - text

Description: a plain text dump of JSON objects. The first row is a header row. Each additional row returned is an individual JSON object. Rows are delimited using a newline (\n) marker, so implementations can read in a single line at a time, handle it, and move on.

To get the data I am using:

response = requests.get(urldetails).text

If I use .json() it errors out with a JSON decode error. The output of the above is something along the lines of:

{data..} {data...}

I am unsure whether each dict is on a separate row, however I am under the impression it's actually just one continuous string as many of my attempts to decode it ended up with an error 'str' object cannot be...etc. . I don't see the '\n' separators anywhere when I am using the .text method.

What's the best way of going about and make each dict a separate item in a list or a row in a dataframe (which I can unpack later).

Thanks


回答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'])



回答2:


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

import json
json_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 from GET request and load as a string
import json
json_resp = [json.loads(line) for line in response.split('\n')]


来源:https://stackoverflow.com/questions/49772489/decode-text-response-from-api-in-python-3-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!