Skip to content

Decoding of datetime values contained in JSON streams. A wrapper around simplejson `loads` method.

License

Notifications You must be signed in to change notification settings

ldaverio/json-datetime

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON-datetime

https://secure.travis-ci.org/nicolaiarocci/json-datetime.png?branch=master

JSON-datetime allows for proper decoding of datetime values contained in JSON streams.

The problem

The JSON standard RFC 4627 does not support datetime types. These are usually represented as strings and Python decoders end up decoding them as such. Consider the following example:

import simplejson as json

>>> test = '{"name": "John Doe", "born": "Thu, 1 Mar 2012 10:00:49 UTC"}'
>>> json.loads(test)

As you can see, in the resulting dictionary born is still a string.

The solution

JSON-datetime is a very simple wrapper around Python simplejson loads method. It decodes datetime values contained in JSON strings:

import jsondatetime as json

>>> test = '{"name": "John Doe", "born": "Thu, 1 Mar 2012 10:00:49 UTC"}'
>>> json.loads(test)
{'name': 'John Doe', 'born': datetime.datetime(2012, 3, 1, 10, 0 ,49)}

Strings are parsed using dateutil.parser.parse which is fairly flexible for common datetime formats

Custom parsing

Being just a wrapper around the loads method, you can still use all the standard loads arguments, object_hook included. This means that you can still perform custom parsing of your inbound JSON stream.

Installation

pip install json-datetime

About

Decoding of datetime values contained in JSON streams. A wrapper around simplejson `loads` method.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%