Passing php array to python , doesn't work
Question: I'm trying to send php array encoded as JSON to python script , it doesn't
work . Here's my code :

    
    
     <?php
    
        $data = array('as', 'df', 'gh');
        // $result = shell_exec('python /path/to/myScript.py ' . escapeshellarg(json_encode($data)));
        $result = system('pythomPath/python scriptPath/myscript.py ' . escapeshellarg(json_encode($data)).' 2>&1',$result);
        // Decode the result
        $resultData = json_decode($result, true);
    
        // This will contain: array('status' => 'Yes!')
        var_dump($resultData);
        ?>
    

python :

    
    
    import sys, json
    # Load the data that PHP sent us
    try:
        data = json.loads(sys.argv[1])
    except:
        print ("ERROR")
        sys.exit(1)
    # Processing
    result = data[0]
    # Sending (to PHP)
    print (json.dumps(result))
    


Answer: The data you're sending to `json.loads` is not a valid JSON, check it. If you
want to convert that array to JSON, just use `json.dumps` as you did in the
end of code.

But that isn't the only error. If you send this array as parameter as
sys.argv, you'll not have expected result. If you send an array to this
script, it will handle this param as a string.

Try this approach to handle it as an list, than to JSON.

    
    
    data = eval(sys.argv[1])[0]
    
    print (json.dumps(data))
    

