I want to convert a dictionary like this:
{
"name": "Paul",
"age": "20",
"gender": "male"
}
to this :
{
name: "Paul",
age: "20",
gender: "male"
}
Basically the same as a JSON, but the object properties cannot be wrapped around quotes. Is it possible to do that in Python?
CodePudding user response:
In the end I did it using regex:
def saveFile(dictionary, fileName):
jsonStr = json.dumps(dictionary, indent=4, ensure_ascii=False);
removeQuotes = re.sub("\"([^\"] )\":", r"\1:", jsonStr);
fileNameCleaned = fileName.split(" ")[0]
with open(fileNameCleaned ".ts", "w",encoding='utf_8') as outfile:
outfile.write("export const " fileNameCleaned " = " removeQuotes ";")
CodePudding user response:
In Python:
import json
send_to_js = json.dumps({
"name": "Paul",
"age": "20",
"gender": "male"
})
Then in JavaScript:
JSON.parse(send_to_js)
// result is {name: 'Paul', age: '20', gender: 'male'}