0

I have the following Python code:

array_to_return = dict()
response_json_object = json.loads(responsestring)

for section in response_json_object:
    if section["requestMethod"] == "getPlayerResources":
        array_to_return["resource_list"] = json.dumps(section["responseData"]["resources"])
        break
array_to_return["requests_duration"] = time.time() - requests_start_time
array_to_return["python_duration"] = time.time() - python_start_time

Which returns the following content into a PHP script:

{'resource_list': '{"aaa": 120, "bbb": 20, "ccc": 2138, "ddd": 8}', 'requests_duration': '7.30', 'python_duration': 41.0}

I'm then trying to decode this string and convert it into something usable in PHP. My code if the following:

$cmd = "$python $pyscript";
exec("$cmd", $output);

echo 'output: ';
var_dump($output[0]);

$json_output = json_decode($output[0], true);

echo 'json_output: ';
var_dump($json_output, json_last_error_msg());

$output[0] is a string but json_last_error_msg() returns Syntax Error

I'm well aware that my string is not a valid Json string, but how can I convert it properly (either in Python or in PHP)? I probably do something wrong in my Python script...

UPDATE 1:

I actually found out that responsestring is a valid JSON string (with double quotes) but json.loads switches the double to single quotes; thus response_json_object has single quotes.

If I comment out the line with json.loads, I get an error:

TypeError: 'int' object is not subscriptable

UPDATE 2:

I managed to get around it by removing the associative list in Python, not exactly what I was hoping for but this works for now...

array_to_return = json.dumps(section["responseData"]["resources"])
#No longer using the following
#array_to_return["requests_duration"] = time.time() - requests_start_time
#array_to_return["python_duration"] = time.time() - python_start_time

If a working solution with associative list is suggested, I will accept that one.

6
  • 1
    The json is invalid, according to jsonlint.com. Json likes double-quotes, not single-quotes, and there should not be quotes around the resource_list object. Commented Jul 23, 2020 at 14:20
  • 1
    Valid JSON does not use single quotes! Did you rewrite the JSON or copy/paste it? Commented Jul 23, 2020 at 14:22
  • You cannot use $output[0] until you convert JSONString to PHP Array using json_decode() Commented Jul 23, 2020 at 14:24
  • The problem is that the single quotes are added automatically by Python it seems... Commented Jul 23, 2020 at 14:29
  • Not a Python Dev BUT I find that Very difficult to believe that Python cannot create JSON properly Commented Jul 23, 2020 at 14:38

2 Answers 2

2

The ' character is not a legal character for JSON, it must be a ".

Your json should look like this.

{
    "resource_list": "{\"aaa\": 120, \"bbb\": 20, \"ccc\": 2138, \"ddd\": 8}",
    "requests_duration": "7.30",
    "python_duration": 41.0
}
Sign up to request clarification or add additional context in comments.

5 Comments

This is true, but it does not actually answer the question as one has to assume that Python can return valid JSON if done properly
And all that escaping is not actually necessary either
Yes, somebody has answered the python solution. I don't know python, so I don't know how to solve the python part. I just answered that the error he is receiving is because the json is not correct. The question is that he "can't decode a json", and showing that he can't decode it because the json is wrong, technically answers the question. But of course, the approved answer must be the one that solves the python code.
So that is really just a comment. A bit like @aynber and I did in the comments above
Technically is an answer. Q: "Can't decode JSON string coming from Python in PHP" A: You can't decode it because is not a JSON
1

instead of modifying the individual key, value pairs of array_to_return by json.dumps, you would json.dumps the whole dictionary.

array_to_return = dict()
response_json_object = json.loads(responsestring)

for section in response_json_object:
    if section["requestMethod"] == "getPlayerResources":
        array_to_return["resource_list"] = json.dumps(section["responseData"]["resources"])
        array_to_return["resource_list"] = section["responseData"]["resources"]
        break
array_to_return["requests_duration"] = time.time() - requests_start_time
array_to_return["python_duration"] = time.time() - python_start_time

json.dumps(array_to_return)

2 Comments

I tried that but get the same problem. See the update Iäve made in my question. The single quotes are their from the beginning. Shall I try to replace them with double-quotes?
see my second update with json.load which adds the single quotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.