deserializing a huge json string to python objects

  • Last Update :
  • Techknowledgy :

Trial 1

In[1]: data = {
   "name": "yolanda",
   "age": 4
}

In[2]: class Person:
   ...: def __init__(self, name, age):
   ...: self.name = name
   ...: self.age = age
   ...:
   In[3]: % % timeit
   ...: Person( ** data)
   ...:
   1000000 loops, best of 3: 926 ns per loop

Trial 2

In[4]: data = {
   "name": "yolanda",
   "age": 4
}

In[5]: class Person2:
   ....: def __init__(self, data):
   ....: self.__dict__ = data
   ....:
   In[6]: % % timeit
   ....: Person2(data)
   ....:
   1000000 loops, best of 3: 541 ns per loop

And you can replace two lines with one with:

obj_key = dct["@uuid"]
del(dct["@uuid"])

Suggestion : 2

Last Updated : 17 May, 2022

Example of JSON Object:

{
   "id": 101,
   "company": "GeeksForGeeks"
}

Complex JSON objects are those objects that contain a nested object inside the other. Example of Complex JSON Object.

{
   "id": 101,
   "company": "GeeksForGeeks",
   "Topics": {
      "Data Structure",
      "Algorithm",
      "Gate Topics"
   }
}

Output: 

{
   "first_name": "Jake",
   "last_name": "Doyle"
}
__main__.GFG_User object at 0x105ca7278

By this line:

json_data = json.dumps(team.__dict__,
   default = lambda o: o.__dict__, indent = 4)

Suggestion : 3

Last updated: Mar 11, 2020 12:40 , Created by Yana Todorova, last modified on Mar 11, 2020

with open("Sample.json", "r") as read_it:
   data = json.load(read_it)
import json

cake = '{"name": "Banana miracle", "Toppings": ["Chocolate", "Mint Frosting"]}'
cake_dict = json.loads(cake)

# Output: {
   'name': 'Banana miracle',
   'Toppings': ['Chocolate', 'Mint Frosting']
}
print(cake_dict)

# Output: ['Chocolate', 'Mint Frosting']
print(cake_dict['Toppings'])
{
   "name": "Bob",
   "toppings": ["Chocolate", "Mint frosting"]
}

# Here 's how you can parse this file:

import json

with open('path_to_file/cake.json') as f:
   data = json.load(f)

# Output: {
   'name': "Banana miracle",
   "Toppings": ["Chocolate", "Mint Frosting"]
}

print(data)
import json

cake_dict = {
   'name': 'Banana miracle',
   'price': 35,
   'Filling': None
}

cake_json = json.dumps(cake_dict)

# Output: {
   "name": "Banana miracle",
   "price": 35,
   "Filling": null
}

print(cake_json)
import json

cake = {
   "name": "Banana miracle",
   "price": 35,
   "Baked": True,
   "Frozen": False,
   "Size": ("small", "big"),
   "Filling": None,
   "Toppings": [{
         "topping": "Chocolate",
         "additional price": 2.5
      },
      {
         "topping": "Mint Frosting",
         "additional price": 4.1
      }
   ]
}

# convert into JSON:
   y = json.dumps(cake)

# the result is a JSON string:
   print(y)
{
   "name": "Banana miracle",
   "price": 35,
   "Baked": true,
   "Frozen": false,
   "Size": ["small", "big"],
   "Filling": null,
   "Toppings": [{
      "topping": "Chocolate",
      "additional price": 2.5
   }, {
      "topping": "Mint Frosting",
      "additional price": 4.1
   }]
}

Suggestion : 4

I am using simplejson to deserialize json string to python objects. I have a custom written object_hook that takes care of deserializing the json back to my domain objects., 2 days ago Jun 16, 2016  · 8. I am using simplejson to deserialize json string to python objects. I have a custom written object_hook that takes care of deserializing the json back to my domain objects. The problem is, when my json string is huge (i.e. the server is returning around 800K domain objects in the form of a json string), my python deserializer is taking ... ,The problem is, when my json string is huge (i.e. the server is returning around 800K domain objects in the form of a json string), my python deserializer is taking almost 10 minutes to deserialize them.,I see that without object_hook the framework returns just a list of dictionaries not list of domain objects.


def _object_hook(dct): if '@CLASS' in dct: # server sends domain objects with this @CLASS clsname = dct['@CLASS'] # This is like Class.forName(This imports the module and gives the class) cls = get_class(clsname) # As my server is in java, I convert the attributes to python as per python naming convention.dct = dict((convert_java_name_to_python(k), dct[k]) for k in dct.keys()) if cls != None: obj_key = None
if "@uuid" in dct obj_key = dct["@uuid"] del(dct["@uuid"])
else: info("Class missing uuid: " + clsname) dct.pop("@CLASS", None) obj = cls( ** dct) #This I found to be the most time consuming process.In my domian object, in the __init__ method I have the logic to set all attributes based on the kwargs passed
if obj_key is not None: shared_objs[obj_key] = obj #I keep all uuids along with the objects in shared_objs dictionary.This shared_objs will be used later to replace references.else: warning("class not found: " + clsname) obj = dct
return obj
else: return dct
{
   "action": "print",
   "method": "onData",
   "data": "Madan Mohan"
}
class payloadstring actionstring methodstring data
>>> j = '{"action": "print", "method": "onData", "data": "Madan Mohan"}' >>>
   import json >>> >>> class Payload(object): ...def __init__(self, j): ...self.__dict__ = json.loads(j)... >>> p = Payload(j) >>> >>> p.action 'print' >>> p.method 'onData' >>> p.data 'Madan Mohan'
class Payload(object): def __init__(self, action, method, data): self.action = action self.method = method self.data = data
import json def as_payload(dct): return Payload(dct['action'], dct['method'], dct['data']) payload = json.loads(message, object_hook = as_payload)
.__dict__

Suggestion : 5

int, float, int- & float-derived Enums,Changed in version 3.4: Added support for int- and float-derived Enum classes.,Deserialize s (a str, bytes or bytearray instance containing a JSON document) to a Python object using this conversion table.,If skipkeys is true (default: False), then dict keys that are not of a basic type (str, int, float, bool, None) will be skipped instead of raising a TypeError.

>>>
import json
   >>>
   json.dumps(['foo', {
      'bar': ('baz', None, 1.0, 2)
   }])
'["foo", {"bar": ["baz", null, 1.0, 2]}]' >>>
print(json.dumps("\"foo\bar"))
"\"foo\bar" >>>
print(json.dumps('\u1234'))
"\u1234" >>>
print(json.dumps('\\'))
"\\" >>>
print(json.dumps({
      "c": 0,
      "b": 0,
      "a": 0
   }, sort_keys = True)) {
      "a": 0,
      "b": 0,
      "c": 0
   } >>>
   from io
import StringIO
   >>>
   io = StringIO() >>>
   json.dump(['streaming API'], io) >>>
   io.getvalue()
'["streaming API"]'
>>>
import json
   >>>
   json.dumps([1, 2, 3, {
      '4': 5,
      '6': 7
   }], separators = (',', ':'))
'[1,2,3,{"4":5,"6":7}]'
>>>
import json
   >>>
   print(json.dumps({
      '4': 5,
      '6': 7
   }, sort_keys = True, indent = 4)) {
      "4": 5,
      "6": 7
   }
>>>
import json
   >>>
   json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')['foo', {
      'bar': ['baz', None, 1.0, 2]
   }] >>>
   json.loads('"\\"foo\\bar"')
'"foo\x08ar' >>>
from io
import StringIO
   >>>
   io = StringIO('["streaming API"]') >>>
   json.load(io)['streaming API']
>>>
import json
   >>>
   def as_complex(dct):
   ...
   if '__complex__' in dct:
   ...
   return complex(dct['real'], dct['imag'])
      ...
      return dct
         ...
         >>>
         json.loads('{"__complex__": true, "real": 1, "imag": 2}',
            ...object_hook = as_complex)
         (1 + 2 j) >>>
         import decimal >>>
         json.loads('1.1', parse_float = decimal.Decimal)
Decimal('1.1')
>>>
import json
   >>>
   class ComplexEncoder(json.JSONEncoder):
   ...def
default (self, obj):
...
if isinstance(obj, complex):
   ...
   return [obj.real, obj.imag]
      ...# Let the base class
default method raise the TypeError
   ...
   return json.JSONEncoder.default(self, obj)
      ...
      >>>
      json.dumps(2 + 1 j, cls = ComplexEncoder)
'[2.0, 1.0]' >>>
ComplexEncoder().encode(2 + 1 j)
'[2.0, 1.0]' >>>
list(ComplexEncoder().iterencode(2 + 1 j))['[2.0', ', 1.0', ']']