Problem with JSON - Python3.4

Asked

Viewed 95 times

2

I am working with the Buscapé API in order to get information from some products on the site, the problem is that I cannot manipulate one of the values of the returned json. Below follows some results I managed to obtain:

resp = buscape.find_product_list(keyword='jogos', format='json') # Fazendo a consulta

resp.keys() # Retornou: 'dict_keys(['data', 'code'])'

type(resp['code']) # Retornou: '<\class 'int>'

type(resp['data']) # Retornou: '<\class 'bytes>'

As you can see, when I checked the type of Resp['data'] (field that holds the values I need) python returned me the value "class bytes", with this, I can even convert this value to string, the problem is that it would be laborious to seek out every piece of information I need in this way .

I’ve tried the dump like this:

import json

json.dumps(resp)

But the integer returns me the following error: is not JSON serializable.

As the json is very large, I left the code hosted in my Pastebin for you to get a better sense of the problem I’m facing.

  • Guys, I was able to solve the problem, I only had to do the Code('utf-8') of the field Resp['data'], so I was able to do json.loads and manipulate the information I needed.

  • 1

    André, put your answer down here (answers section) in case someone wants to know also about this problem!

  • Solved your doubt?

1 answer

0

As far as I can tell, all you have to do is use the method .Decode to convert bytes to Unicode. See if you can do this conversion as follows:

import json

json.dumps(resp['data'].decode('utf-8'))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.