-1

I am trying to count the number of Unicode characters in the JSON data. I am using requests to get the data from the feed.

import requests

r = requests.get('https://venmo.com/api/v5/public?since=1438578858&until=1438578958')'
j_data = r.text

Now, I need to convert the j_data into a dictionary to get the message items alone. If I just use json.loads(j_data), I get UnicodeEncodeError: 'charmap' codec can't encode character.

Therefore, I am encoding the j_data and then trying to convert to dict using loads. I am getting this error

TypeError: the JSON object must be str, not 'bytes'

How to approach this problem?

Code:

import requests
import json

r = requests.get('https://venmo.com/api/v5/public?since=1438578858&until=1438578958')

j_data = r.text

encoded = j_data.encode()

b = json.loads(encoded)
print(b)
PyAn
  • 271
  • 3
  • 13
  • Can't you just use the `json` attribute of the response object (`r`) instead of decoding it yourself‽ – BlackJack Aug 05 '15 at 10:13

3 Answers3

0

It seems to work fine in Python 2.7.6

import requests
import json

req = requests.get('https://venmo.com/api/v5/public?since=1438578858&until=1438578958')
contentJ = json.loads(req.content)

and I get a dict named contentJ

Wyc0
  • 1
  • 3
0

As I see it, you try to encode something, that does not need to be encoded. Stripping of the line with the encoding and all works fine in Python3.4.

import requests
import json

r = requests.get('https://venmo.com/api/v5/public?since=1438578858&until=1438578958')

j_data = r.text

b = json.loads(j_data)
print(type(b))
Oliver Friedrich
  • 8,425
  • 7
  • 38
  • 47
0

To get json, use r.json():

import requests # $ pip install requests

r = requests.get(url)
data = r.json()

Your error: UnicodeEncodeError: 'charmap' codec can't encode character. is unrelated to the json parsing. Most likely it happens when you are trying to print Unicode to Windows console. Configure the console font that can display the desired characters and install win-unicode-console package:

T:\> py -mpip install win-unicode-console
T:\> py -mrun your_script_that_prints_unicode.py

See What's the deal with Python 3.4, Unicode, different languages and Windows?

Community
  • 1
  • 1
jfs
  • 346,887
  • 152
  • 868
  • 1,518