What am I doing wrong? Python3:
>>> import json
>>> s = "\"{'key': 'value'}\""
>>> obj = json.loads(s)
>>> obj['key']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string indices must be integers
Given JSON string is produced by json_encode()
from php
Real string:
{sessionid:5,file:\/home\/lol\/folder\/folder\/file.ttt}
.
UPDATE: Problem is because i transfer json string as command line to shell_exec
in php, and read it using sys.argv[1]
... So quotes are removed by shell.
PHP Code (which runs python script with shell_exec
)
$arg = json_encode(['sessionid' => $session->getId(), 'file' => $filename]);
shell_exec('python3 script.py ' . $arg);
Python code:
import json, sys
s = json.loads(sys.argv[1]) #fails already