2
dct_data = json_tour_data.__dict__
tour_data = json.dumps(dct_data)

How to remove these backslashes from json? Here is my output:

"{\"strFileOpenDateAjxKey\": \"2018-01-16 12:40:22.526417\", 
\"strFilePassengerAjxKey\": \"Zahra Walji\", \"strFileOpenMobileAjxKey\": 
\"46464664\", \"strFileOpenDepartmentAjxKey\": \"Finance department\", 
\"strFileOpenAccountCodeAjxKey\": \"CARTZS\", 
\"strFileOpenProfileCodeAjxKey\": \"CARTZS\", 
\"strFileopenOriginalCountryIdAjxKey\": 61, \"blnBoundAjxKey\": 1, 
\"strTransactionCurrencyJsKey\": \"Shillings\", 
\"intCurrencyPrecisionJsKey\": 3, \"strPackageTypeJsKey\": \"PKG\", 
\"strUserNameAjxKey\": \"admin\", \"strPasswordAjxKey\": \"1234\"}"
krezus
  • 1,281
  • 1
  • 13
  • 29
Jamshy
  • 60
  • 1
  • 11

4 Answers4

6

The answer is you have to just play with json.dumps() and json.loads().

The following is my code which worked:-

import json
json_data = {'key1': 'first'}
json_data = json.dumps(json_data)
return {
    'statusCode': 200,
    'schools':  json.loads(json_data)
}

The output of above code is as follows:

Response:
{
  "schools": {
    "key1": "first"
  },
  "statusCode": 200
}
Rushabh Sudame
  • 414
  • 1
  • 6
  • 22
2

I would recommend checking your Response object where your parse your tour_data variable/dictionary in your views. I originally had the same issue as you but here's what I changed.

Original implementation: Response(json.dumps(a_dictionary), status=status.HTTP_200_OK)

to

New implementation: Response(a_dictionary, status=status.HTTP_200_OK, content_type='json')

The key things here are: 1. Getting rid of the json.dumps conversion method and just pass through a plain python dictionary e.g. see a_dictionary. 2. Setting content_type='json' on the Response object.

Todd Drinkwater
  • 379
  • 6
  • 17
1

you can use replace("\'", '"') for that.

     json = '''{\"strFileOpenDateAjxKey\": \"2018-01-16 12:40:22.526417\", 
      \"strFilePassengerAjxKey\": \"Zahra Walji\", \"strFileOpenMobileAjxKey\": 
      \"46464664\", \"strFileOpenDepartmentAjxKey\": \"Finance department\", 
      \"strFileOpenAccountCodeAjxKey\": \"CARTZS\", 
      \"strFileOpenProfileCodeAjxKey\": \"CARTZS\", 
      \"strFileopenOriginalCountryIdAjxKey\": 61, \"blnBoundAjxKey\": 1, 
      \"strTransactionCurrencyJsKey\": \"Shillings\", 
      \"intCurrencyPrecisionJsKey\": 3, \"strPackageTypeJsKey\": \"PKG\", 
      \"strUserNameAjxKey\": \"admin\", \"strPasswordAjxKey\": \"1234\"}'''
newString = json.replace("\'", '"')
print(newString)

check from here

that is the output when pressed the run in my side. output

krezus
  • 1,281
  • 1
  • 13
  • 29
  • There is no issue when i am trying from terminal using pdb. but while retrieve the service using the RESTer there is the same issue exists. – Jamshy Jan 16 '18 at 07:38
  • i tried your link (https://repl.it/repls/VillainousRemorsefulSulphurbutterfly). but there is backslashes in out put. like when i try my own terminal using pdb. – Jamshy Jan 16 '18 at 07:42
  • 1
    hey.. #krezus my issue solved. Thank you for your answer. i just removed json.dumps(dct_data). because it is already in json format – Jamshy Jan 16 '18 at 08:39
  • This can be solution but not a correct way to do it. – Rushabh Sudame Oct 14 '20 at 21:06
  • @RushabhSudame you don't have to vote down even if you think that answer is not the way should be. It might have helped someone and might help again. – krezus Oct 15 '20 at 07:19
0

To anyone else who might be facing this issue.

simply put: the original question seems to be taking JSON data (I emphasize, it's already JSON) and rendering it into JSON again.

dct_data = json_tour_data.dict

tour_data = json.dumps(dct_data)

Jamal
  • 11
  • 1
  • 2