JSON string as script parameter

Hi,

I want to pass a json string as a script parameter. e.g.

--json_string='["a", { "x": 0.4, "y": 0.15, "z": 0.35 }]'

This works fine from command line but PyCharm converts the string to

'[a,{x:0.4,y:0.15,z:0.35}]'

Some help would be nice.
8 comments
Escape double quotes with the backslash: i.e. –json_string='[\"a\" ...

If you're editing script parameters in a run/debug configuration dialog then should be the "Help" button.
0

This is not working work me!

PyCharm 5.0.5

Build #PY-143.2370, built on April 30, 2016

Contents:
--json_string='["a", { "x": 0.4, "y": 0.15, "z": 0.35 }]'

Result:
'--json_string=[a, { x: 0.4, y: 0.15, z: 0.35 }]'

Content:
--json_string='[\"a\", { \"x\": 0.4, \"y\": 0.15, \"z\": 0.35 }]'

Result:
'--json_string=[\\"a\\", { \\"x\\": 0.4, \\"y\\": 0.15, \\"z\\": 0.35 }]'

 

I need to have the quotes but not the backslashes

0

Basically, there are only two rules:
- double quotes prevent a string from being split on whitespaces
- escaped quotes are passed as-is

So, you should write the argument like this:

--json_string="[\"a\", { \"x\": 0.4, \"y\": 0.15, \"z\": 0.35 }]"
0

Your example able on Nov 2013 shows ' and not ". I have tried with ' and did not work. Here is what I am doing

Running PyCharm 5.0.5
Build #PY-143.2370, built on April 30, 2016

Script Parameter:
--json_string='["a", { "x": 0.4, "y": 0.15, "z": 0.35 }]'

Result:
'--json_string=[a, { x: 0.4, y: 0.15, z: 0.35 }]'

Script Parameter:
--json_string='[\"a\", { \"x\": 0.4, \"y\": 0.15, \"z\": 0.35 }]'
Result:
'--json_string=[\\"a\\", { \\"x\\": 0.4, \\"y\\": 0.15, \\"z\\": 0.35 }]'

0

This is super annoying - I thought there was something wrong with my python - turns out it is pycharm, because my script runs fine from command line.  So now I've been working all afternoon to try to figure out pycharm, instead of my actual work.

0

As a get around we do this

json_args = json.loads(sys.argv[2].replace('~', '"'))

Then in PyChar we use ~ in place of the quotes.

 

Not ideal but it allows us to use the tool.

0

Thank you! - that does get me past my initial pycharm issue.  It doesn't resolve the follow-on issue however.

This json validates on my go-to json validator (http://pro.jsonlint.com), but stack traces in pycharm.

{
"username": "flow",
"detection_name": "W32.EICAR.16g1\\r",
"password": "password",
"sharight": "e1105070ba828007508566e28a2b8d4c65d192e9eaf3b7868382b7cae747b397\\r",
"tanium_host": "sandbox.local"
}

raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Using ipython, I don't have the same issue.

In [6]: mydictstring = '{"username":"flow","detection_name":"W32.EICAR.16g1\\r","password":"password","sharight":"e1105070ba828007508566e28a2b8d4c65d192e9eaf3b7868382b7cae747b397\\r","tanium_host":"sandbox.local"}'

In [7]: print mydictstring
{"username":"flow","detection_name":"W32.EICAR.16g1\r","password":"password","sharight":"e1105070ba828007508566e28a2b8d4c65d192e9eaf3b7868382b7cae747b397\r","tanium_host":"sandbox.local"}

In [8]: newargs = json.loads(mydictstring)

In [9]: print newargs
{u'username': u'flow', u'password': u'password', u'detection_name': u'W32.EICAR.16g1\r', u'sharight': u'e1105070ba828007508566e28a2b8d4c65d192e9eaf3b7868382b7cae747b397\r', u'tanium_host': u'sandbox.local'}

In [10]: print type(newargs)
<type 'dict'>

I would add that if I just set the json as a variable at the top of my script, rather than trying to use the pycharm input "capability", it works just fine.

So far, my pycharm experience has not been a good one (read I'm almost ready to give up and go back to using vim / ipython.  I do appreciate you responding to my cry for help. =)

0

Guys,

quite naturally, PyCharm does not support all the quoting and escaping features of a shell. But it is able to pass whatever parameters you want. Please, just open the dialog and spend a minute on a help page - it contains an example of quoting a JSON parameter.

0

Please sign in to leave a comment.