5

So I have JSON request with format like this:

{
"item_ids": [635,10,692,194,9412],
"gender": "male",
"number_results": 5
}

I'm trying to parse array in "item_ids". But I got error message like in the title. This is my code:

resto_id = json.loads['item_ids']
data  = json.dumps(resto_id)

I also tried this:

response = requests.get("http://127.0.0.1:8520/recommend_multi")
users = json.loads(response.text)
data = users['item_ids']

But gave me an error:

TypeError: Object of type JSONDecodeError is not JSON serializable

Edit: Maybe this will help:

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

    dct={}
    new_user = 'newusername'
    try:
        e=""
        resto_id = json.loads['item_ids']
        data  = json.dumps(resto_id)
        # response = requests.get("http://127.0.0.1:8520/recommend_multi")
        # users = json.loads(response.text)
        # data = users['item_ids']
        
        gender = request.json['gender']
        resto_rec = float(request.json['number_results'])

        input_dict = {'id_resto': data,
        'gender': [gender, gender, gender, gender, gender], 'username': [new_user, new_user, new_user, new_user, new_user]}
        
        dct = {"items": input_dict}
        dct2 = {"data": dct, "message":"sukses", "success":True}

    except Exception as e:
        dct2 = {"data": dct, "message":e, "success":False}
    

    return jsonify(dct2)

And this is the traceback: enter image description here enter image description here I run it with docker. And for request I'm using Insomnia

4
  • Can you share reproducible code that we can run? And also the full traceback if possible Commented Jun 24, 2021 at 1:09
  • You mean like a full code? Commented Jun 24, 2021 at 1:22
  • Not necessarily. Just code that resembles your real code, and we can run that gives the same error you are getting. Commented Jun 24, 2021 at 1:25
  • I do some edit. Sorry I post the traceback with images because it says too many codes in my post Commented Jun 24, 2021 at 1:37

2 Answers 2

3

The problem is in this snippet:

    except Exception as e:
        dct2 = {"data": dct, "message":e, "success":False}

You are basically trying to JSON serialize the exception e which is not possible. You need to use something that is JSON serializable, like the string representation of the exception, for example by using str(e):

    except Exception as e:
        dct2 = {"data": dct, "message":str(e), "success":False}
Sign up to request clarification or add additional context in comments.

9 Comments

It works thanks. But then I got the exception message 'function' object is not subscriptable
Can you edit your problem description and add traceback as well? But judging from the code, it should be because of json.loads['item_ids']. loads is a function, not something subscriptable like a dictionary
The code is running, no error message. It seems catch an error above so it gave me a response from except condition. This is the response { "data": {}, "message": "'function' object is not subscriptable", "success": false }
If you temporarily remove the try except, it should tell you which line is causing the problem
Yeah, but for handling json request format above, am I already right for getting array data in "item_ids"? Because I can't found example for getting array data in "item_ids" in my case and with json request format like above
|
0

First, thanks to @bdbd to keep responding to me. The exception solution helped me to fix my code, but in the end I managed to debug my code then found the solution which is resolve my problems about retrieving array in JSON objects. So instead of this:

resto_id = json.loads['item_ids']
data  = json.dumps(resto_id)

I need to request first.

resto_id = request.json
data  = json.dumps(resto_id)
data2 = json.loads(data)
#Then retrieve the array
data2["item_ids"]

1 Comment

Actually you can just do request.json()['item_ids']. No need to dump it and then load it back.

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.