1

I use Databricks to get delta table info and I return the result as a JSON. The return goes by return json.dumps(value). However, when I receive the JSON it looks like 'key': 'value', with single quotes. Also, the booleans are displayed as True, False while null is displayed as None (which is normally since this is the way Python returns things). After receiving it into Python (precisely, Flask app), I forward this JSON to my .NET app but before that, I clean it with the following way:

json_value = json_value.replace("'", '"')
json_value = json_value.replace('True', 'true')
json_value = json_value.replace('False', 'false')
json_value = json_value.replace('None', 'null')

This thing worked for 2 months until one of my records got Cote d'Ivore as a value. This 'cleaning' replaced the single quote here with a double quote and that made my JSON crash on the .NET side. I parse the JSON on the .NET side by JsonConvert.DeserializeObject(response.Content).

Now, is there some automatic way to parse the JSON correctly at the Python side, without having to clean it by replacing and have a proper functionality when I'd send it to .NET?

3
  • 1
    How did you create json_value? It should not be a string at this point, but a deserialized JSON object (such as list or dict). Then the forwarding can trivially be done via json.dumps(). Commented Mar 2, 2022 at 12:25
  • 1
    It looks like your json_value is string. You should use dict intead, and then it is properly converted to json by json.dump Commented Mar 2, 2022 at 12:25
  • Yes, json_value is a string. I see, will try it out. Thanks! Commented Mar 2, 2022 at 12:26

1 Answer 1

1

You need to pass a dictionary object in json.dumps() instead of a string.

import json
a={'s':'asdas','fff':23,'ss':True, 'sssss':None}
res=json.dumps(a)
print (res)
##OUTPUT
## {"s": "asdas", "fff": 23, "ss": true, "sssss": null}

Assuming that you have string instead of a python dictionary; in that case you can convert that to dictionary using ast library

import ast
import json
value="{'s': 'asdas', 'fff': 23, 'ss': True, 'sssss': None}"
 #assuming it is in string format
value=ast.literal_eval(value) #convert string format dictionary to dictionary 
result=json.dumps(value)


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

Comments

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.