Skip to main content

A library for serialising and deserialising URL query strings which can represent hierarchical data structures which can also be represented as JSON. This is convenient, for example, if you wish to have a web API which takes JSON objects as a POST body, but which may also need to respond to GET requests for which a more readable set of parameters would be appropriate.

Project description


jsonurl is a simple library for managing the serialisation and deserialisation of URL query strings which represent hierarchically structured data.

It will take python data structures which can be represented in JSON and convert them to flat key/value pairs for use in URL query strings, and it will take URL query strings and parse them into hierarchical data structures.

This is convenient if you wish to pass structured data around in the URL itself rather than in a POST body. This may complement a web API which also accepts JSON strings in the body of a POST request, providing equivalent functionality using GET without constructing URLs which include url-encoded json objects.


>>> import jsonurl

### Converting Data Structures

A simple flat dictionary

>>> d = {"one" : 1, "two" : 2}
>>> jsonurl.query_string(d)

A dictionary with a nested dictionary

>>> d = {"one" : {"two" : 2, "three" : 3}, "four" : 4}
>>> q = jsonurl.query_string(d)
>>> q

We can also get a flat dictionary as a proxy for the hierarchic one, if we do not want jsonurl to actually prepare the query part of the URL for us

>>> jsonurl.dict_to_args(d)
{'four': '4', 'one.three': '3', 'one.two': '2'}

We can parse the query string back into a python dictionary object

>>> jsonurl.parse_query(q)
{'four': 4, 'one': {'three': 3, 'two': 2}}

We can also serialise nested lists:

>>> d = {"one" : 1, "two" : [2,3,4]}

The flattened data structure:

>>> jsonurl.dict_to_args(d)
{'two.1': '3', 'two.0': '2', 'two.2': '4', 'one': '1'}

The query string itself:

>>> q = jsonurl.query_string(d)
>>> q

This can be comfortably parsed back into a python data structure

>>> jsonurl.parse_query(q)
{'two': [2, 3, 4], 'one': 1}

We can go on to serialise arbitrarily complex data structures

>>> d = {"one" : [ {"two" : 2, "three" : 3}, 4 ]}
>>> q = jsonurl.query_string(d)
>>> q

### Escape Characters

jsonurl will also handle url-escaping:

>>> q = {"escape_me" : "I'll need escaping"}
>>> s = jsonurl.query_string(q)
>>> s

jsonurl uses “+” for a space instead of %20 to improve readability

It also unescapes everything during parse:

>>> jsonurl.parse_query(s)
{'escape_me': "I'll need escaping"}

Since jsonurl uses “.” as its separator, dictionary keys with “.” in the name are escaped. We prefix each “.” with another during serialisation:

>>> d = {"user.names" : ["richard", "jones"]}
>>> q = jsonurl.query_string(d)
>>> q

“..” is then converted back during parsing:

>>> jsonurl.parse_query(q)
{'user.names': ['richard', 'jones']}

### Parameter Ordering

It orders the query parameters, to make it easier to read long query strings:

>>> d = {"b" : "last", "a" : [1,2,3]}
>>> jsonurl.query_string(d)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonurl-1.0.0.tar.gz (3.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page