I have A method for format the output as JSON.
My keyword_filter will be pass in this this format:
<QueryDict: {u'customer_type': [u'ABC'], u'tag': [u'2']}>
<QueryDict: {u'customer_type': [u'TDO'], u'tag': [u'3']}>
<QueryDict: {u'customer_type': [u'FRI'], u'tag': [u'2,3']}>
In fact this I got from request.GET (keyword_filter=request.GET)
This is my method: (I am trying)
def save_fiter_to_JSON(self, dest, keyword_filter):
fwrite = open(dest, 'a')
#keyword_filter = <QueryDict: {u'customer_type': [u'FRI'], u'tag': [u'2,3']}>
string_input1 =string.replace(str(keyword_filter), '<QueryDict:', '["name:"')
string_input2 = string.replace(string_input1, '>', '')
fwrite.write(string_input2+",\n")
fwrite.close()
The JSON format that I want:
[
{"name": filter_name, "customer_type": "ABC", "tag": [2,3]},
]
Or the other good one format from you.
import simplejson as json
>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
'["foo", {"bar": ["baz", null, 1.0, 2]}]'
filter_name will be passed from the method save_fiter_to_JSON.
Some tips:
QueryDictto to Python dictionary withdict(keyword_filter)expression,dict(keyword_filter, name=filter_name)expression.Then use
jsonmodule to dump JSON and write it to the file.