(May-02-2019, 06:06 AM)buran Wrote: It's fair to guess thatrows
is returned from query to database, so it is list/tuple of tuples. You need to convert it to dict, before pass tojson.dumps()
import json rows = [(1, "Apple"), (2, "Banana"), (3, "Orange")] print(json.dumps(rows)) print(json.dumps(dict(rows)))
Output:[[1, "Apple"], [2, "Banana"], [3, "Orange"]] {"1": "Apple", "2": "Banana", "3": "Orange"} >>>
Thanks Buran,
You are correct in that this is results from a query. The code also executes inside a AWS Lambda function.
Ive modified my code to
return { 'statusCode': 200, 'headers': {'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json'}, "body": json.dumps(dict((rows)) }But am getting syntax error
Im told that output should be
{ "body": [{ "Id”: "1”, "Fruit”: "Apple” },{ "Id”: "2”, "Fruit”: "Banana” }] }