0


I'm trying to send an array (with a length of 45,000) from PHP to Python and back.
From PHP to Python:

$a = shell_exec('/usr/bin/python2.7 script.py "'.$b.'" '.escapeshellarg(json_encode($array))); 

From Python to PHP:

B = json.loads(sys.argv[1])
print json.dumps(B)

And in the PHP code:

$output = json_decode($a);
$arrfinal = array_values($output);

Hope this is make sense..
So i try to run it for 3700 rows of the array and it works just fine.
if i try to run it for more than 3700 rows it gives me a syntex error (from json_last_error())

So my question is, Is there anyway I can check if string will make a json syntex error if will be "changed" to json format before it "change"?

Thanks!

P.S already try changing everything to UTF-8 after hours of searching online..

Sorry for my English..

Yaman Jain
  • 1,254
  • 11
  • 16
Aviv
  • 193
  • 2
  • 11
  • 1
    Would wrapping `json.loads` in a `try..except` block work for you? – randomir Aug 19 '17 at 17:46
  • If you have a string that may or may not be valid JSON, you can put `json.loads()` inside a try/except block. If you get a `ValueError` exception, then it wasn't valid. – John Gordon Aug 19 '17 at 17:46
  • But I'm creating the Json from an array (that being created in the Python). So if I'm going to wrap it with try except the all array is not going to be vaild because one of the rows.. – Aviv Aug 19 '17 at 18:07
  • JSON is either valid or it isn't. If one row is bad then the entire JSON is bad. – ryantxr Aug 19 '17 at 18:15
  • I would suggest doing a base64 encoding of your JSON before sending it. – ryantxr Aug 19 '17 at 18:27
  • @ryantxr I try to chage everything to base64, but the text (in base64!!) still create a bad Json.. How is it possible?? – Aviv Aug 19 '17 at 18:53
  • @ryantxr maybe i can check for this type of "bad" strings from the mysql? at this point i will try anything!! – Aviv Aug 19 '17 at 18:57
  • You need to divide and conquer. PHP should be able to create JSON and read that JSON it produced. First make sure that PHP can generate the JSON and read the JSON it created. After that, make sure that python can decode the JSON it received. – ryantxr Aug 19 '17 at 19:00
  • What's the size (in bytes) of your JSON (encoded array)? Maybe you're hitting the [limits of program arguments length](https://stackoverflow.com/questions/19354870/bash-command-line-and-input-limit). What system are you running this on? – randomir Aug 19 '17 at 22:33
  • 1
    @randomi Seems like 3700 is my limit. Will check the post thanks! – Aviv Aug 20 '17 at 03:38

0 Answers0