Flask/Keras: <p>Failed to decode JSON object: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)</p>

Viewed 392

I have created a simple Keras model and implemeted it to an API with Flask, unfortunately I am getting the error shown below:

C:\Users\callu\OneDrive\Documents\Keras>curl  -d "{"9","7","9","5","7","6","0","4","6","0","4","7","0","7"}" -H "Content-Type: application/json" -X POST http://localhost:5000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>Failed to decode JSON object: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)</p>

I have tried switching out the arrangment of the data with " or ' but have had no luck(i'm not an expert in JSON so have been following online tutorials)

Here is the curl command:

curl  -d "{"9","7","9","5","7","6","0","4","6","0","4","7","0","7"}" -H "Content-Type: application/json" -X POST http://localhost:5000
pause

And here is the the Python code:

from math import expm1

import joblib

import numpy as np

from flask import Flask, jsonify, request

from tensorflow import keras

from keras.models import load_model

from keras.models import model_from_json

import pandas as pd

app = Flask(__name__)

app.config["DEBUG"] = True


json_file = open('CatModel1.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("CatModel1.h5")

print("Model Loaded")

@app.route("/", methods=["POST"])

def index():

    input = request.json

    prediction = loaded_model.predict(input)

    return jsonify({"Taste": str(df)})
app.run()
1 Answers

You will need to escape the double quotes that are inside your JSON.

Eg. "{ \"9\", \"7\", \"9\", \"5\", \"7\"}"

What it's currently seeing is "{" then 9 then "," then 7 then "," etc.

You could also try using a single quote as the outside quote but YMMV, it depends on how your environment works.

Eg. '{ "9", "7", "9", "5", "7"}'

Also since this appears to be a list of numbers, they may not need quotes at all.

Eg. '{9, 7, 9, 5, 7}'

And finally since it's a list, use square braces and not curly braces. The curly braces are for key-value pairs such as {"name": "Bob"}

[9, 7, 9, 5, 7]
Related