I need to convert a json-string to python object. By object I mean "new" python3 object like:
class MyClass(object):
I found several help for example on jsonpickle documentation. But all I found are tutorials which convert object to json first and after this convert backwards.
I want to convert a json-string from a Rest-API.
Here is what I have done so far:
import requests
import jsonpickle
class Goal(object):
def __init__(self):
self.GoaldID = -1
self.IsPenalty = False
class Match(object):
def __init__(self):
self.Goals = []
headers = {
"Content-Type": "application/json; charset=utf-8"
}
url = "https://www.openligadb.de/api/getmatchdata/39738"
result = requests.get(url=url, headers=headers)
obj = jsonpickle.decode(result.json)
print (obj)
This results in:
TypeError: the JSON object must be str, bytes or bytearray, not 'method'
It's quite clear to me that jsonpickle can't convert this to my classes (Goal, Match), because I don't tell jsonpickle in which class the output should be converted. The problem is I don't know how to tell jsonpickle to convert the JSON in object from type Match? And how can I tell that the list of goals should be of type List<Goal>
?
The following lines will give you a dictionary:
obj = jsonpickle.decode(result.content) # NOTE: `.content`, not `.json`
obj = result.json()
But none of above will give you what you want (python object (not dicitonary)). because the json from the url is not encoded with jsonpickle.encode
- whcih add additional information to a generated json (something like {"py/object": "__main__.Goal", ....}
)
>>> import jsonpickle
>>> class Goal(object):
... def __init__(self):
... self.GoaldID = -1
... self.IsPenalty = False
...
>>> jsonpickle.encode(Goal())
'{"py/object": "__main__.Goal", "IsPenalty": false, "GoaldID": -1}'
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# JSON encoded with jsonpickle.encode (default unpicklable=True)
# => additional python class information attached
# => can be decoded back to Python object
>>> jsonpickle.decode(jsonpickle.encode(Goal()))
<__main__.Goal object at 0x10af0e510>
>>> jsonpickle.encode(Goal(), unpicklable=False)
'{"IsPenalty": false, "GoaldID": -1}'
# with unpicklable=False (similar output with json.dumps(..))
# => no python class information attached
# => cannot be decoded back to Python object, but a dict
>>> jsonpickle.decode(jsonpickle.encode(Goal(), unpicklable=False))
{'IsPenalty': False, 'GoaldID': -1}
If you want an actual Python object which is not a dictionary, i.e. you prefer dic.Goals.[0].GoalGetterName
to dic["Goals"][0]["GoalGetterName"]
, use json.loads
with object_hook:
import json
import types
import requests
url = "https://www.openligadb.de/api/getmatchdata/39738"
result = requests.get(url)
data = json.loads(result.content, object_hook=lambda d: types.SimpleNamespace(**d))
# OR data = result.json(object_hook=lambda d: types.SimpleNamespace(**d))
goal_getter = data.Goals[0].GoalGetterName
# You get `types.SimpleNamespace` objects in place of dictionaries
Do you mean something like this?
import json
class JsonObject(object):
def __init__(self, json_content):
data = json.loads(json_content)
for key, value in data.items():
self.__dict__[key] = value
jo = JsonObject("{\"key1\":1234,\"key2\":\"Hello World\"}")
print(jo.key1)
which prints:
1234
[Finished in 0.4s]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With