Home > Software design >  Converting list with json dumps returns string instead of data dictionary
Converting list with json dumps returns string instead of data dictionary

Time:10-03

I am trying to get json data from Request URL using requests. Result I get is string so first I use json loads to convert it to list and after that I use json dumps to convert it to data dictionary. But instead I get string again. I would like to get it as data dictionary.

This is the code I use:

from json import loads
import json


url = 'https://www.sberbank.hr/umbraco/api/ExchangeRates/GetRates?dateString=1633159401811'

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


jsondata = loads(data)

print(type(jsondata))

data_dict = json.dumps(jsondata)

print(type(data_dict))

CodePudding user response:

It works exactly like how it described in the document: https://docs.python.org/3/library/json.html#json.dumps

I think your jsondata is already an array of objects, what are you trying to archive here?

CodePudding user response:

When you use json.loads, it converts thejson string to a dict type and json.dumps converts dict to json string. Just use json.loads if you want a dict.

I've inspected the data you are using. The structure is:

List[Dict]

So you can acess it like this data[0] or any index.

  • Related