0
cursor = connection.cursor()
cursor.execute('SELECT patientFirstName, patientLastName FROM `patientmaster` WHERE nik = "1"')
patientProfile = cursor.fetchall()

patientProfileArray = []
patientName = None

for row in patientProfile:
    patientFirstName = row[0]
    patientLastName = row[1]

patientProfile = {'Patient': [{'patientFirstName': patientFirstName, 'patientLastName': patientLastName}]}

responseObject = {}
responseObject['statusCode'] = 200
responseObject['headers'] = {}
responseObject['body'] = json.dumps(patientProfile, default=str)
return responseObject

and it returns

{\"Patient\": [{\"patientFirstName\": \"vin\", \"patientLastName\": \"wan\"}]}

how do i remove the \ in json format

wkl
  • 77,184
  • 16
  • 165
  • 176
  • 2
    Those `\` do not exist in the actual JSON format, they're added by string representation. Where do you see that output? – bereal Jun 21 '22 at 15:38
  • i use an api and call it with postman – Kevin Irawan Jun 21 '22 at 15:42
  • does this help https://stackoverflow.com/questions/5997029/escape-double-quotes-for-json-in-python ? – Sean C Jun 21 '22 at 15:52
  • not really sir i want to clean the json response from all those \ – Kevin Irawan Jun 21 '22 at 15:56
  • if it's a string then just do ```s.replace('\\', '')``` where s is the string. The backslashes are just escaping the double quotes. (The first backslash escapes the second, letting Python know it's a literal '\' , not escaping the following " – Sean C Jun 21 '22 at 16:01
  • can i create the json response without the \ sir? because I'm creating a json currently and I want it to look like a normal json format – Kevin Irawan Jun 21 '22 at 16:05
  • I don't know. Read up on escapes in json.dumps() ? – Sean C Jun 21 '22 at 16:15
  • 2
    It's still not clear what you are doing and what you see. Escaping with backslashes typically happens when you display a quoted string that contains quote characters. That does not mean that the backslashes are part of the string, only that this specific way of displaying them adds them for clarity. That's also the reason why your description is too vague to tell. All we know is that they are somewhere where you don't expect them, but that's not enougt to tell whether your expectations are wrong or the API response is wrong. Please provide a [mcve], read [ask] and take the [tour]! – Ulrich Eckhardt Jun 21 '22 at 16:27

0 Answers0