Python serializing and deserializing to json



Here a some quick sample code to work with json objects in python.

Deserializing to object


commands = json.loads(commandstrings)
commandResultList = []

for command in commands:
o = CommandType(**command)
commandResultList.append(o)



Serializing to Json


import json

cmd1 = CommandType("list", "ls", "al")
cmd2 = CommandType("list", "pwd", "")
cmd3 = CommandType("list", "ls", "www.google.com")

cmds = [cmd1, cmd2, cmd3]
a = json.dumps(cmds, default=lambda o: o.__dict__)

Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm