max max - 5 months ago 18
JSON Question

Expecting value on the json.loads

in my app am calling a php script which will return a json encoded string

output = str(subprocess.check_output(["php","-f" , "../connector/somefile.php" ,"code_string:"+code_string]));
print(output)


here is my print(output)

b'{"success":"1","code":"pewcndIiD8WcB5MdH+8JOAVJ03xlt92sIUYgs2m2tIGtLohnDHjBP8s8x9\\/YroJnyrCLcyN6Brwicn9\\/DS4j9kH8a6gcOKFS1bpmvdKxJSrmgT0aO
z0u16npbt1ldklXx16WEybEN4gKG0DI="}'


when i try to decode it into a dictionary

dic = json.loads(output)


i get

ValueError at /myapp/

Expecting value: line 1 column 1 (char 0)


whats wrong with this line ? isn't it a valid json ? i pretty sure im generating valid json object in the php file but i'm not sure what the extra
b
is in the begining of json string in the python side

Answer

'b stands for byte-string, if you decode it, it should work

json.loads(output.decode('utf-8'))
{'code': 'pewcndIiD8WcB5MdH+8JOAVJ03xlt92sIUYgs2m2tIGtLohnDHjBP8s8x9/YroJnyrCLcyN6Brwicn9/DS4j9kH8a6gcOKFS1
bpmvdKxJSrmgT0aOz0u16npbt1ldklXx16WEybEN4gKG0DI=', 'success': '1'}