abden003
abden003

Reputation: 1335

Parse the following query string into json using python

I have a query string with the following format:

 cmd=get-records&limit=100&offset=0&search[0][field]=number&search[0][type]=text&search[0][operator]=contains&search[0][value]=Mike+Jones&search[1][field]=name&search[1][type]=text&search[1][operator]=contains&search[1][value]=Mike+Jones&search[2][field]=role&search[2][type]=text&search[6]&searchLogic=OR

How can I convert this to structured json like the following (or similar):

{
  cmd: "...",
  limit: "...",
  offset: "...",
  search: {
    0: {
      number: "..."
      name: "...",
      ...
    }
    1: {
      ...
    }
    ...
  }, 
  ...
}

I have tried to use urlparse.parse_qs but it translates the query string to the following:

{
  "cmd": ["..."],
  "limit": ["..."],
  "offset": ["..."],
  "search[0][number]": ["..."],
  "search[0][name]": ["..."],
  "search[1][number]": ["..."].
  ...
}

The problem with this is the search fields. I want this to be correctly structured. The technologies I am using are as following:

Frontend:

w2ui table that requests data from the backend. Also, as shown in this example, when doing a search it sends a request to the backend to do the search.

Backend:

Django. The post request from w2ui is handled by a view which takes in the query string and acts accordingly.

Upvotes: 0

Views: 2215

Answers (3)

Cyphase
Cyphase

Reputation: 12022

Take a look at the querystring-parser package, which does exactly what you need:

import pprint

from querystring_parser import parser as qsparser

# I had to modify the query string you provided by adding a value
# search[6]; it didn't have one before, which caused an exception
query_string = (
    'cmd=get-records&limit=100&offset=0&search[0][field]=number&'
    'search[0][type]=text&search[0][operator]=contains&'
    'search[0][value]=Mike+Jones&search[1][field]=name&search[1][type]=text&'
    'search[1][operator]=contains&search[1][value]=Mike+Jones&'
    'search[2][field]=role&search[2][type]=text&search[6]=SEARCH6&'
    'searchLogic=OR'
    )  # NOTE: I had

query_string_as_dict = qsparser.parse(query_string)

pprint.pprint(query_string_as_dict)

The result is:

{u'cmd': u'get-records',
 u'limit': 100,
 u'offset': 0,
 u'search': {0: {u'field': u'number',
                 u'operator': u'contains',
                 u'type': u'text',
                 u'value': u'Mike Jones'},
             1: {u'field': u'name',
                 u'operator': u'contains',
                 u'type': u'text',
                 u'value': u'Mike Jones'},
             2: {u'field': u'role', u'type': u'text'},
             6: u'SEARCH6'},
 u'searchLogic': u'OR'}

If you want it as JSON:

import json

json_string = json.dumps(query_string_as_dict)

Upvotes: 2

Perhaps take a look at this library: querystring-parser

It takes section[1]['words'][2]=a&section[0]['words'][2]=a&section[0]['words'][2]=b and converts it to {u'section': {0: {u'words': {2: [u'a', u'b']}}, 1: {u'words': {2: u'a'}}}} which looks like what you're after.

Their docs for using it within Django:

from querystring_parser import parser
post_dict = parser.parse(request.GET.urlencode())

Upvotes: 1

Pasqual Guerrero
Pasqual Guerrero

Reputation: 406

If you're using Django, you have this information structured like this on request.GET inside the view.

Upvotes: 0

Related Questions