Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert json to object?

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>?

like image 880
Sebi Avatar asked Mar 08 '17 12:03

Sebi


2 Answers

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
like image 85
falsetru Avatar answered Oct 06 '22 02:10

falsetru


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]
like image 37
Den1al Avatar answered Oct 06 '22 02:10

Den1al