0

I'm taking a text-input from html being json object and trying to work upon that. But when I'm trying the following code, I'm getting error/(page not rendering) in encoding and decoding JSON.

@app.route('/', methods=['POST'])
def my_form_post():

    text = request.form['text']
    #getting text-input as text = {'a':'1','b':'2'}

    json_input = json.dumps(text)
    ordered_json = json.loads(text, object_pairs_hook=ordereddict.OrderedDict)
    print ordered_json
    processed_text = htmlConvertor(ordered_json)
    #rep(jso)
    return render_template("my-form.html",processed_text=processed_text)

But when I'm tying to do so with a local JSON variable as jso everything working fine. The same input when I provide with html-input, it's giving an error and I can;t even see the error except displaying Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

 @app.route('/', methods=['POST'])
        def my_form_post():

            jso = '''{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}'''

        json_input = json.dumps(jso)
        ordered_json = json.loads(jso, object_pairs_hook=ordereddict.OrderedDict)
        print ordered_json
        processed_text = htmlConvertor(ordered_json)
        #rep(jso)
        return render_template("my-form.html",processed_text=processed_text)

UPDATE:

Everything working fine now, but for the integers, it isn't working. For eg:

{"name":"yo","price":"250"}

works perfectly but

{"name":"yo","price":250}

ain't. What's the solution for that? Any specific answer or I would have to check for integer in python and then convert it to string before applying any JSON related methods and functioning.

2
  • please add the full traceback you get in console Commented Sep 17, 2013 at 14:19
  • @PaoloCasciello: I didn't get any traceback since my debug mode wasn't ON and yes, turning it on and using double quotes solves my problem Commented Sep 18, 2013 at 21:18

1 Answer 1

1

Not sure if this is your problem, but {'a':'1','b':'2'} is not a valid JSON object because of the single quotes:

>>> json.loads("{'a':'1','b':'2'}")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode
    obj, end = self._scanner.iterscan(s, **kw).next()
  File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan
    rval, next_pos = action(m, context)
  File "/usr/lib64/python2.6/json/decoder.py", line 171, in JSONObject
    raise ValueError(errmsg("Expecting property name", s, end))
ValueError: Expecting property name: line 1 column 1 (char 1)

If instead you use double quotes everything works fine:

>>> json.loads("{\"a\":\"1\",\"b\":\"2\"}")
{u'a': u'1', u'b': u'2'}

Also note that to get stack traces intead of code 500 errors when there is an exception you have to start your flask server as follows:

app.run(debug = True)
Sign up to request clarification or add additional context in comments.

1 Comment

app.run(debug = True) gives me the full insight and yep, double quotes solve the whole problem. Thanks for pointing that out

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.