2

Ruby on Rails JSON is able to parse out curl calls in the following format:

name=test. It will interpret this as {"name": "test"}.

Python's JSON seems to take this as a JSON error (which it obviously is). Is there a way however, to take parse payload in the format of name=test? I'm using Python's JSON with Flask here.

1
  • This function to parse query strings might be useful perhaps? Commented Aug 6, 2013 at 20:23

2 Answers 2

3

If you are using flask, why not use jsonify

from flask import jsonify

@app.route('/do')
def do_whatever():
    return jsonify(name=test)

This will send a JSON response like this to the browser:

{
    "name": "test",
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is useful, thanks but I want to modify the attributes first and then return them in a JSON format. This seems to return a response object. name=test is passed as payload/data via curl. How can I parse that data as "name":"test", or a json/dict??
Here's an example of a curl call: curl -X POST http://localhost:5000 -d name=somename -d age=15. I've done this in Rails before, but not sure how to in Flask.
1

Try

fields = curl_str.split('=')
curl_json = { fields[0]:fields[1] }

1 Comment

What if my request contained: curl .... -d name=test -d age=12 -d location=somecity? This won't parse out all the data, will it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.