jquery: post with json will actually post array - python

I have a python as CGI and the POST from jquery will transform json object to array, so when I see the POST from jquery, I actually see:
login_user[username]=dfdsfdsf&login_user[password]=dsfsdf
(the [ and ] already escaped)
My question is how I can convert this string back to JSON in python? Or, how can I convert this string to python array/dict structure so that I can process it easier?
[edit]
My jquery is posting:
{'login_user': {'username':username, 'password':password}}

If what you want to accomplish is to send structured data from the browser and then unpack it in your Python backend and keep the same structure, I suggest the following:
Create JavaScript objects in the browser to hold your data:
var d = {}
d['login_user'] = { 'username': 'foo', 'password': 'bar' }
Serialize to JSON, with https://github.com/douglascrockford/JSON-js
POST to your backend doing something like this:
$.post(url, {'data': encoded_json_data}, ...)
In your Python code, parse the JSON, POST in my example is where you get your POST data in your CGI script:
data = json.loads(POST['data'])
data['login_user']

import re
thestring = "login_user[username]=dfdsfdsf&login_user[password]=dafef"
pattern = re.compile(r'^login_user\[username\]=(.*)&login_user\[password\]=(.*)')
match = pattern.search(thestring)
print match.groups()
Output:
>>> ('dfdsfdsf', 'dafef')
Thus,
lp = match.groups()
print "{'login_user': {'username':"+lp[0]+", 'password':"+lp[1]+"}}"
shall bear: >>> {'login_user': {'username':dfdsfdsf, 'password':dafef}}

>>> import json
>>> data = {'login_user':{'username':'dfdsfdsf', 'password':'dsfsdf'}}
>>> json.dumps(data)
'{"login_user": {"username": "dfdsfdsf", "password": "dsfsdf"}}'
I suspect that data would already be contained in a GET var if that's coming from the URL...

Related

Get json from url by string indice Flask

Url : 127.0.0.1:5000/mywebservice?&list=hello&list=world&dict={"type":"mail","subject":"Hello","content":"hello world"}
I am trying to get json from the url
list=request.args.getlist(list) allow me to get list like this way list[0] =hello ,list[1] =world
the URL will be like that:
Url : 127.0.0.1:5000/mywebservice&list=hello&list=world&dict[type]=mail&dict[subject]=Hello&dic[content]=hello world
Is there a way that allows me to get the dictionary like this way?
Expected result:
dic=request.args.get(dict)
var= dic['type']
print(var)====> mail
however:
dic = request.args.get(dict)
var = dic['type']
print(var) : Exception
PS: request.json doesn't have access to the url and there is not
dic = request.args.getjson(dict)
If I understood correctly dict from URL will have serialized JSON. You need to use json package to de-serialize it.
import json
url_dict = request.args.get('dict')
dic = json.loads(url_dict)
print (dic['type'])

JSON looking string (or "\" in output) of Flask Restful/Pymongo JSON output

First, reference links to other questions I read through. They might be clues to what I am experiencing, although I am not understanding enough yet to see the solution to my problem.
How can I use Python to transform MongoDB's bsondump into JSON?
Unable to deserialize PyMongo ObjectId from JSON
I've got a Flask Restful API I'm working on. An excerpt of my code is as follows:
class DeviceAPI(Resource):
def get(self, deviceID):
# do the query
deviceCollection = db['device']
device = deviceCollection.find_one({'deviceID': deviceID})
print device #1
print ''
print json_util.dumps(device) #2
print ''
s = json_util.dumps(device)
print s #3
print ''
results = {}
results['device'] = s
print results #4
# respond
return results #5
At Print #1, I get the following, and I understand and expect this.
{u'deviceID': u'ABC123', u'_id': ObjectId('....')}
At Print #2 and #3 are identical outputs as expected, and again I understand and expect this (I think).
{"deviceID": "ABC123", "_id": {"$oid": "...."}}
Print #4 has an added key in the dictionary. However, it looks like the value of the key:value is a string where it should be the dictionary as in #2 and #3.
{'device': '{"deviceID": "ABC123", "_id": {"$oid": "...."}}'}
The returned result, #5, according to CURL is along the lines of the following. There are the added / in the there. I suspect because of #4 value looking like a string and that continues in #5 as well.
{"device": "{\"deviceID\": \"ABC123\", \"_id\": {\"$oid\": \"....\"}}"}
I'm trying to get a pure JSON output, not a string representation of the device document. #2 and #3 looked like JSON, but in #4 became a string. Why? And how to do this correctly?
I believe it's because json_utils.dumps is converting your device variable into a string when you should be just returning a complete json object. You essentially end up returning something that resembles this:
return {"device": "a string that resembles json"}
Instead, modify your code to look like this:
class DeviceAPI(Resource):
def get(self, deviceID):
# do the query
deviceCollection = db['device']
device = deviceCollection.find_one({'deviceID': deviceID})
results = {'device': device}
return results
Now, we're returning json that looks more like this:
return {"device": {"deviceID": "ABC123", "_id": {"$oid": "...."}}}
However, it looks like the recommended way to return json in flask is to actually use the flask.jsonify method so that Flask will return a proper response object:
from flask import jsonify
class DeviceAPI(Resource):
def get(self, deviceID):
# do the query
deviceCollection = db['device']
device = deviceCollection.find_one({'deviceID': deviceID})
return jsonify(device=device)
Michael0x2a helped to clear some fog in my mind, and after more experimenting and thinking this through, the following is working for me.
results['device'] = json.loads(json_util.dumps(device))
In my experimenting I was using json_util.dumps and json_util.loads. But I didn't recognize that while json_util.dumps was converting the BSON like item to JSON string, that json_util.loads was converting it directly back. Therefore, another function was needed to take the string like JSON output of json_util.dumps and make it into a dictionary JSON like object. Thus a combo was needed. This is using "import json" and "from bson import json_util" (of Pymongo).
Thank you for your reply
I had the same problem as you, and it was fixed just because you post back the solution, thanks million! Follow below my code
from flask_restplus import Namespace, Resource, fields
from .. import mongo
import json
from bson import json_util
api = Namespace('inventory', description='Store management related operations')
#api.route('/items')
class Inventory(Resource):
def get(self):
inventory_collection = mongo.db.inventory
resp = inventory_collection.find({})
a = json.loads(json_util.dumps(resp))
return {'result': a }

Parse requests.post object

How can i parse the entire output takes from a request.post object and extract only the "id" content, considering this piece of code?
import json
import requests
API = 'https://www.googleapis.com/urlshortener/v1/url'
elem = json.dumps({'longUrl':'http://www.longurl..'})
output = requests.post(API,elem, headers = {'content-type':'application/json'})
adding output.text it gives me this:
{
"kind": "urlshortener#url",
"id": "http://goo.gl/..",
"longUrl": "http://www.longurl.."
}
now I just need to extract the link in the id field, i also tried to put the content in a file and parse it as strings with file.read() but seems not work. Any ideas?
Load it into dictionary using json module:
data = json.loads(output.text)
print data['id'] # prints http://goo.gl/O5MIi

Generating json in python for app engine

I am somewhat new to python and I am wondering what the best way is to generate json in a loop. I could just mash a bunch of strings together in the loop, but I'm sure there is a better way. Here's some more specifics. I am using app engine in python to create a service that returns json as a response.
So as an example, let's say someone requests a list of user records from the service. After the service queries for the records, it needs to return json for each record it found. Maybe something like this:
{records:
{record: { name:bob, email:blah#blah.com, age:25 } },
{record: { name:steve, email:blah#blahblah.com, age:30 } },
{record: { name:jimmy, email:blah#b.com, age:31 } },
}
Excuse my poorly formatted json. Thanks for your help.
Creating your own JSON is silly. Use json or simplejson for this instead.
>>> json.dumps(dict(foo=42))
'{"foo": 42}'
My question is how do I add to the
dictionary dynamically? So foreach
record in my list of records, add a
record to the dictionary.
You may be looking to create a list of dictionaries.
records = []
record1 = {"name":"Bob", "email":"bob#email.com"}
records.append(record1)
record2 = {"name":"Bob2", "email":"bob2#email.com"}
records.append(record2)
Then in app engine, use the code above to export records as json.
Few steps here.
First import simplejson
from django.utils import simplejson
Then create a function that will return json with the appropriate data header.
def write_json(self, data):
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(simplejson.dumps(data))
Then from within your post or get handler, create a python dictionary with the desired data and pass that into the function you created.
ret = {"records":{
"record": {"name": "bob", ...}
...
}
write_json(self, ret)

Parsing response from Github

How do I make string like "payload={'key':'value'...}" to become a variable payload with value assigned to it "{'key':'value'...}"? Thanks.
Like they said on github, use a query string parser, like this:
import urlparse
str = "payload={'key':'value'}"
print urlparse.parse_qs(str)
Result is this:
{'payload': ["{'key':'value'}"]}
payload = json.loads(jsonstring)
payload = pickle.loads(pickstring)
Don't pickle untrusted data.

Categories