0

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.

abden003
  • 1,325
  • 7
  • 24
  • 48
  • The first format is not strictly JSON. All JSON object keys are strings. All JSON strings are double quoted. I can see how the keys and values shown do not fit the requirements of an application that requires more structure, but the quotes around a string are correct. See http://json.org/ – Paul Aug 11 '15 at 07:05

3 Answers3

2

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)
Cyphase
  • 11,502
  • 2
  • 31
  • 32
  • 1
    Beat me to it! This library looks great, but it's a shame arrays are being parsed as dictionary's. – Ashley 'CptLemming' Wilson Aug 11 '15 at 07:49
  • 1
    @Ashley'CptLemming'Wilson, in this case it wouldn't work as a list, but I just tried changing the `6` to a `3`, and it's still a dict. I suppose it could detect incrementing keys starting from `0`, but there's not much additional benefit in doing that anyway, if any. – Cyphase Aug 11 '15 at 07:53
1

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())
0

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