Exclude empty/null values from JSON serialization

def del_none(d): “”” Delete keys with the value “None“ in a dictionary, recursively. This alters the input so you may wish to “copy“ the dict first. “”” # For Python 3, write `list(d.items())`; `d.items()` won’t work # For Python 2, write `d.items()`; `d.iteritems()` won’t work for key, value in list(d.items()): if value is None: del … Read more

How to install SimpleJson Package for Python

I would recommend EasyInstall, a package management application for Python. Once you’ve installed EasyInstall, you should be able to go to a command window and type: easy_install simplejson This may require putting easy_install.exe on your PATH first, I don’t remember if the EasyInstall setup does this for you (something like C:\Python25\Scripts).

SimpleJSON and NumPy array

In order to keep dtype and dimension try this: import base64 import json import numpy as np class NumpyEncoder(json.JSONEncoder): def default(self, obj): “””If input object is an ndarray it will be converted into a dict holding dtype, shape and the data, base64 encoded. “”” if isinstance(obj, np.ndarray): if obj.flags[‘C_CONTIGUOUS’]: obj_data = obj.data else: cont_obj = … Read more

Reading JSON from SimpleHTTPServer Post data

Thanks matthewatabet for the klein idea. I figured a way to implement it using BaseHTTPHandler. The code below. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer import SocketServer import simplejson import random class S(BaseHTTPRequestHandler): def _set_headers(self): self.send_response(200) self.send_header(‘Content-type’, ‘text/html’) self.end_headers() def do_GET(self): self._set_headers() f = open(“index.html”, “r”) self.wfile.write(f.read()) def do_HEAD(self): self._set_headers() def do_POST(self): self._set_headers() print “in post method” … Read more

Why does json serialization of datetime objects in python not work out of the box for datetime objects

No it doesn’t work that way in json module. The module provides you with a default encoder: json.JSONEncoder. You need to extend this to provide your implementation of default method to serialize objects. Something like this: import json import datetime from time import mktime class MyEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, datetime.datetime): return int(mktime(obj.timetuple())) return … Read more

Python JSON encoding

Python lists translate to JSON arrays. What it is giving you is a perfectly valid JSON string that could be used in a Javascript application. To get what you expected, you would need to use a dict: >>> json.dumps({‘apple’: ‘cat’, ‘banana’:’dog’, ‘pear’:’fish’}) ‘{“pear”: “fish”, “apple”: “cat”, “banana”: “dog”}’

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)