0

Below is the error code I received on my Terminal call of "python lastYearArray.py". (I'm on a MacBook Pro running OSX 10.9.5, editing my Python program in jEdit). According to this link: TypeError: expected string or buffer it appears that the function I am using doesn't take lists either. I would just be working with the JSON file instead of converting it, but this particular JSON data has no headers or element tags, such as "fruits: apple, banana, orange"; it only has a long string of numbers separated by commas and braces.

Traceback (most recent call last):
      File "lastYearAnalysis.py", line 8, in <module>
        PyListData = json.loads(lastYearArray)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads
        return _default_decoder.decode(s)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    TypeError: expected string or buffer

However,I don't know why it is telling me about a decoder, because as you can see from my rather minimal code here:

#Write json array to a python list to find the iterative location of max value
#should return a value such as "[n]" on req for max

import requests
import json

lastYearArray = requests.get('https://api.github.com/repos/mbostock/d3/stats/contributors')
PyListData = json.loads(lastYearArray)
print data[PyListData]

#TODO: ask for max value

I'm not using an explicit decoder function that I can see; I'm using json.loads. I should add that I am certainly a Python novice with regards to lists and JSON array decoding. Any info, however basic, that you can provide would be immensely helpful to me.

Thank you.

1
  • You can simply edit your question and add the missing information to it. Commented Apr 3, 2016 at 15:37

2 Answers 2

1

requests.get returns a Response object. That object is not a string object, so you cannot pass it to json.loads.

You have to get the response object’s using response.text, and pass that to json.loads:

lastYearArrayResponse = requests.get('…')
data = json.loads(lastYearArrayResponse.text)

Alternatively, you can also use the response.json() method to get the parsed response since requests already comes with built-in support for JSON responses:

lastYearArrayResponse = requests.get('…')
data = lastYearArrayResponse.json()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks very much for the response! So, you're saying that json.loads actually creates a local file and puts all the data there? If that's true then a) where does it put the file? and b) how can I search the file for a maximum? I am only familiar with Java arrays, so I don't really know how python arrays work in terms of sequencing, especially because they're called "lists." I need to be able to search that file for the maximum value and return the place of the highest value within the list so that I can calculate what day that commit was on. Can I do a max search with the code I have now?
What? Where did you take the “creates a local file” part from? No, absolutely not. You are trying to parse some JSON response from a webservice; the webservice returns a string. You parse that string as an in-memory object using json.loads() (or response.json()). That returns then an object that represents that content. If the response was a list of things, then that deserialized object is a list. Once deserialized, you can handle that like every other list.
0

You forget to add .text at the end of line lastYearArray = to get the HTML source of the webpage.

Also, I think the last line is not correct...

1 Comment

Thanks for the response!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.