Python Forum
expecting value: line 1 column 1 (char 0) in print (r.json))
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
expecting value: line 1 column 1 (char 0) in print (r.json))
#1
hi i experience a problem. So i deploy a model in flask


from flask import Flask, request, jsonify#--jsonify will return the data
from keras.models import load_model
import numpy as np
import pandas as pd

#creates the web service running on port 127.0.0.1:12345 and answer post requests on address of the machine/api
app = Flask(__name__)

model=load_model('modelfordeploy.h5')

@app.route("/")
def hello():
    return "machine learning model APIs!"


@app.route('/api', methods=['GET','POST'])
def predict():
    
    # get the json send from the post as data
    data=request.get_json(force=True)
    print (data)
    
    # i want 6 variables to make the prediction
    #i expected jason to have the following values
    predict_request=[data["accx"], data["accy"], data["accz"], data["gyrx"], data["gyry"], data["gyrz"]]
    print (predict_request)
    
    predict_request= np.array(predict_request)
    print (predict_request)
    type (predict_request)
    
    predict_request = np.asarray( predict_request).reshape(-1, 1, 6)

    pred= model.predict(predict_request)
    print (pred)
    
    output=[pred[0]]
    print (output)
    
    #take a list of dictionaries and convert them to json
    return jsonify(results=output)
   

if __name__ == '__main__':
    app.run(port=8000, debug=True) #threaded=False)
and then make a post request

import requests, json
import pandas as pd
import numpy as np

url="http://127.0.0.1:8000/api"

data= json.dumps ({"accx":0.660583, "accy":0.454468, "accz":-0.585022, "gyrx":32.366615, "gyry":27.206556, "gyrz":-23.471800})
r=requests.get(url, data)
print (r.json())
as I result I take back the following error. I can figure out how to fix it
Error:
File "request.py", line 22, in <module> print (r.json()) File "C:\Users\aggelos\anaconda3\envs\tensorflow_env\lib\site-packages\requests\models.py", line 898, in json return complexjson.loads(self.text, **kwargs) File "C:\Users\aggelos\anaconda3\envs\tensorflow_env\lib\json\__init__.py", line 348, in loads return _default_decoder.decode(s) File "C:\Users\aggelos\anaconda3\envs\tensorflow_env\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\aggelos\anaconda3\envs\tensorflow_env\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) (tensorflow_env) C:\Users\aggelos\Desktop\new_try>
Reply
#2
hi
I am slightly confused here as you mention that the second piece of code is posting a request and yet you are using the requests GET function. If you are looking to post that data variable object in the second piece of code then the parameters for that function are requests.post(url, data, json), I have not looked at the requests package specifically to see what they appertain to.
At the moment you are trying to decode the data expected from the GET which I will be not there i.e. None and hence the char(0),the variable r in the post will actually contain the response code I expect something like r: <Response [500]> for example if the whole thing failed and consequently you can not json that either so just chane it for a print® if you want to test the response result
Regards
-------- *
“Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read.”
Reply
#3
instead of r.json(), try to print r.text so you can see what you really get
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Hi.

I replicated this in my own environment and as said your GET when you invoke the request is received by your api code which then tries to retrieve the body of the request (line 20 first code set)but the second parameter for requests.get package is request parameters aka ?x=12345 style query parameters,if as i suspect you wish to post the dictionary data in the second code set then the request needs to be a post method, this then is retrieved by the code on code set 1 line 20, and although failed for me the rest of the logic, it does return to the "r" variable the response code from flask, in my case HTTP 500, but worked to a fashion.
Regards
-------- *
“Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read.”
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Scraping from multiple URLS to print in a single line. jb89 4 3,304 Jan-29-2020, 06:12 AM
Last Post: perfringo
  Error when decoding JSON ( Expecting ',' delimiter:) tortoise 7 46,939 Feb-18-2019, 08:27 AM
Last Post: tortoise
  os.rename Windows remove illegal char fgerrata 3 8,689 Jan-03-2018, 04:39 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020