php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70445 json_encode bug
Submitted: 2015-09-07 10:24 UTC Modified: 2015-09-20 04:22 UTC
From: fedor at support-pc dot org Assigned:
Status: No Feedback Package: JSON related
PHP Version: 5.6.13 OS: ubuntu and debian
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: fedor at support-pc dot org
New email:
PHP Version: OS:

 

 [2015-09-07 10:24 UTC] fedor at support-pc dot org
Description:
------------
error to converting array to json
I have a big array, generated by php. Data for array i recive from another server by rest API. Then i use json_encode function and write result to file.
But some of array value cant correct convert by slashing.
example of generated array: https://openitstudio.ru/php-error-report/input.array
example of generated json: https://openitstudio.ru/php-error-report/output.json
For example on line 33168 in input.array:
[FatherName] => Yur'evna
string "Yur'evna" convert not correct.
Has no escaping char "'"

Test script:
---------------
function object_to_array($obj) {
    if(is_object($obj)) $obj = (array) $obj;
    if(is_array($obj)) {
        $new = array();
        foreach($obj as $key => $val) {
            $new[$key] = object_to_array($val);
        }
    }
    else $new = $obj;
    return $new;
}
//recive from API a big oject
$rueser = CRunetGateUser::Get($f_UF_RUNET);
//generate array (result in file https://openitstudio.ru/php-error-report/input.array)
$dump['u'.$f_UF_RUNET][] = object_to_array($rueser);
$arr = json_encode($dump);
//in anoter script:
$runetidUesers = json_decode($arr);
//have error on line 33168 in input.array


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-09-07 10:37 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2015-09-07 10:37 UTC] requinix@php.net
Do you expect to see Yur\'evna? JSON does not need to escape 's.

Do you get an error with json_decode()? What is the error message?
  json_decode(file_get_contents("https://openitstudio.ru/php-error-report/output.json"))
works correctly for me.
 [2015-09-07 10:57 UTC] fedor at support-pc dot org
-Status: Feedback +Status: Open
 [2015-09-07 10:57 UTC] fedor at support-pc dot org
Yes, i expect see Yur\'evna, but i see Yur'evna.
Final error:
PHP Parse error:  syntax error, unexpected 'evna' (T_STRING) in /var/www/html/experts/participants.php on line 2,
 [2015-09-07 11:19 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2015-09-07 11:19 UTC] requinix@php.net
That error message means you were trying to execute something as PHP code. Using json_decode() alone will not cause it.

"//in anoter script:"

Use the code in that script to create a short repro for your problem.

<?php

$dump = array("FatherName" => "Yur'evna");
$arr = json_encode($array);

$runetidUesers = json_decode($arr);
// repro code goes here

?>
 [2015-09-20 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 01:01:28 2024 UTC