Search code examples
pythonlinuxechosimplejson

echo json to textfile removes double quotes


I'm trying to write a json dumps string using linux bash shell echo in a text file. My problem is it removes all double quotes.

example code.

d = {"key": "value"}
"echo %s > /home/user/%s" % (simplejson.dumps(d), 'textfile'))

Output in textfile

{key: value}

It removes all double quotes so I can't load it to json because it is not a valid json anymore.

Thanks


Solution

  • You need to escape quotes for Bash usage:

    ("echo %s > /home/user/%s" % (simplejson.dumps(d), 'textfile')).replace('"', '\\"')