0

I'm working on a Django app and I'm trying to redirect based on a response from an API, but I can't seem to find a way to get the value of the response.

This is the response:

{
"Body": {
    "stkCallback": {
        "MerchantRequestID": "24915-30681242-1",
        "CheckoutRequestID": "ws_CO_150220211305438433",
        "ResultCode": 0,
        "ResultDesc": "The service request is processed successfully.",
        "CallbackMetadata": {
            "Item": [
                {
                    "Name": "Amount",
                    "Value": 1.00
                },
                {
                    "Name": "MpesaReceiptNumber",
                    "Value": ""
                },
                {
                    "Name": "TransactionDate",
                    "Value": 20210215130604
                },
                {
                    "Name": "PhoneNumber",
                    "Value": 
                }
            ]
        }
    }
}

Found a way to get the request but instead of returning as dict it returns a str even though i used json.loads().

getting result_code throws an error string indices must be integers

def callback(request):

    callback = json.dumps(request.body.decode('utf-8'))

    body_data = json.loads(callback)

    print(type(body_data)) #returns 'str'

    result_code = body_data["Body"]["stkCallback"]["ResultCode"]

    print(result_code)  #returns string indices must be integers

return render(request, 'callback.html')
4
  • Please share your full traceback. Commented Feb 15, 2021 at 21:50
  • Ensure you are getting JSON returned from API call, also this response is not valid JSON Commented Feb 15, 2021 at 22:21
  • How do i convert it to JSON with python? Commented Feb 16, 2021 at 8:32
  • @JosphatGitogo- Depends on the datatype you are receiving. Check the datatype that is being sent by the API Commented Feb 16, 2021 at 8:49

1 Answer 1

1

@josphat Gitogo - Shouldn't it be resultcode = response['Body']['stkCallback']['ResultCode']

try:
    response = json.loads(request.body).decode('utf-8')
    resultcode = response['Body']['stkCallback']['ResultCode']
except ValueError as v:  # includes simplejson.decoder.JSONDecodeError
   #log the exception or print it

Assuming response =

{
"Body": {
    "stkCallback": {
        "MerchantRequestID": "24915-30681242-1",
        "CheckoutRequestID": "ws_CO_150220211305438433",
        "ResultCode": 0,
        "ResultDesc": "The service request is processed successfully.",
        "CallbackMetadata": {
            "Item": [
                {
                    "Name": "Amount",
                    "Value": 1.00
                },
                {
                    "Name": "MpesaReceiptNumber",
                    "Value": ""
                },
                {
                    "Name": "TransactionDate",
                    "Value": 20210215130604
                },
                {
                    "Name": "PhoneNumber",
                    "Value":
                }
            ]
        }
    }
}
}

Also, I see one of your Value attribute has no value.

 {
                    "Name": "PhoneNumber",
                    "Value":
                }
Sign up to request clarification or add additional context in comments.

2 Comments

Tried your solution but it still throws this error. JSONDecodeError at /api/v1/online/lipa/callback/
Exception Type: JSONDecodeError Expecting value: line 1 column 1 (char 0)

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.