2

I have a chalice application and in that chalice application, I have a lambda function. This lambda function is the data source for my AppSync application. When I make a request and return the response from the Lmabda function, the JSON object is returned as string, and not as JSON object. I have tried so many times, but nothing worked.

This is what I am returning from the lambda function:

'''
 result = {
                    'service': service,
                    'version': version,
                    'requestID': request_id,
                    'result': {
                        'creditDecisionRecommendation': credit_decision_recommendation,
                        'creditScore': bureau_score,
                        'creditLimit': limit
                    }
                }
'''

...and this is what I get as the response:

'''
{
  "data": {
    "postKaubamaja": {
      "service": "kaubamaja-custom-scoring",
      "version": "1.0",
      "result": "{\"creditDecisionRecommendation\":\"accept\",\"creditScore\":10.8,\"creditLimit\":612.1275}"
    }
  }
}
'''

The "result" object should be a JSON object itself, but gets a string. Does someone have any idea what is wrong here?

Thanks

1
  • I've waisted one day on this, it shouldn't be so complicated. I tried changing the result type from String to AWSJSON, lambda resolver returning a object or a json string and even play with the response template. Commented Feb 24, 2023 at 11:32

1 Answer 1

1

If I understand your question correctly, to turn the dictionary into JSON string just use json.dumps. Given the following input:

 result = {
                    'service': 'A',
                    'version': 'B',
                    'requestID': 'C',
                    'result': {
                        'creditDecisionRecommendation': 'D',
                        'creditScore': 123,
                        'creditLimit': 4
                    }
                }

you just need this line:

import json
json.dumps(result)

and gives the JSON string representation of the dictionary result (which is a JSON object):

'{"service": "A", "version": "B", "requestID": "C", "result": {"creditDecisionRecommendation": "D", "creditScore": 123, "creditLimit": 4}}'

Note: In python, each dictionary is by nature a JSON object.

Sign up to request clarification or add additional context in comments.

3 Comments

Add the complete code of your function, with input and expected output
@uuzzairr agreed, please post your source if you would like a resolution.
@Massifox I know how to convert a string into JSON and vice versa. I have tried different options. I am using AppSynch with lambda function as a data source and using GraphQL to query the result from the API. What's happening is that it turns one of the dictionary objects into string even though I return it as a dictionary. I tried several approaches, but nothing is working. Maybe it is something to do with GraphQL?

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.