CLI: Error when storing a json object with python
dkioroglou opened this issue · comments
Description of bug
When trying to store a json object with python using the subprocess module and calling skysh
, skytable returns an error.
Steps to reproduce
#!/usr/bin/env python3
import json
import subprocess
JSON = {"foo": "bar"}
command = ['skysh', '-e', 'set foo {}'.format(json.dumps(JSON))]
response = subprocess.run(command, capture_output=True, text=True)
print(response.stdout)
I get the error:
[Syntax Error: expected whitespace near end of: `{`]
Expected behavior
json.dump()
converts a python dictionary into a string. So, it is expected to be stored in skytable. For instance, the following command works:
command = ['skysh', '-e', 'set foo bar']
Additional observations
Within the REPL the following works:
set foo '{"foo": "bar"}'
However, from the command line the following doesn't work:
$ skysh -e 'set foo {"foo": "bar"}'
It returns the error:
[Syntax Error: expected whitespace near end of: `{`]
This seems to be a bash syntax concern. If you ran the following command in bash:
skysh -e "set foo '{\"foo\": \"bar\"}'"
You will see that it works because we allow the json string to be enclosed by quotes to prevent the whitespace from being incorrectly parsed.
In skysh it looks like this:
set foo '{"foo": "bar"}'
Your python script can work as follows:
import json
import subprocess
JSON = {"foo": "bar"}
command = ['skysh', '-e', 'set foo \"{}\"'.format(json.dumps(JSON, separators=(',', ':')))]
print(command)
response = subprocess.run(command, capture_output=True, text=True)
print(response.stdout)
In this case we wrap the {}
with quotes and remove spaces from the json string by passing separators=(',', ':')
.
To me this seems like expected behavior.
Thank you kensj for your response.
I can confirm that your suggestion for bash worked.
skysh -e "set foo '{\"foo\": \"bar\"}'"
I can also confirm that your suggestion for python worked too. It needed a small correction though.
The following did not work:
command = ['skysh', '-e', 'set foo \"{}\"'.format(json.dumps(JSON, separators=(',', ':')))]
the double quotes should be changed to single quotes. Then it works.
command = ['skysh', '-e', 'set foo \'{}\''.format(json.dumps(JSON, separators=(',', ':')))]
I tried the above command without specifying the separators
keyword and also worked.
command = ['skysh', '-e', 'set foo \'{}\''.format(json.dumps(JSON))]
Parsing the above stored json in python worked.
import json
import subprocess
command = ['skysh', '-e', 'get foo']
response = subprocess.run(command, capture_output=True, text=True)
JSON = json.loads(eval(response.stdout).decode())
print(JSON['foo'])