php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52420 json_decode fails when underscores used in names
Submitted: 2010-07-23 22:13 UTC Modified: 2010-07-23 23:47 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: php at calendee dot com Assigned:
Status: Not a bug Package: JSON related
PHP Version: 5.3.3 OS: Mac OS 10.6.3, RHEL 5
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: php at calendee dot com
New email:
PHP Version: OS:

 

 [2010-07-23 22:13 UTC] php at calendee dot com
Description:
------------
There appears to be a bug in json_decode().  In some cases, if there is an 
underscore in one of the names in a JSON string, json_decode fails to retrieve the 
value.  If the underscore is replaced with nothing, json_decode properly retrieves 
the value.  However, some names with underscores work properly.  






In Depth Explanation : http://calendee.com/json_decode.php

Test script:
---------------
URL : http://calendee.com/json_decode.txt

<?php

$string = '{"alarm_groups_id":"0","start":"2010-07-23 10:23:13","stop":"2010-07-23 12:23:13","model":"","pairs_id":"","alarms_alarm_num":"1044","stps_abbr":"dlls","alarms_lsn":"bm700145","slc":"","card":"","port":"","linksets_company":"","opc":"","dpc":"","aftpc":"","feclli":"","cdpa_addr":"","cdpa_ssn":"","cdpa_tt":"","cgpa_addr":"","cgpa_ssn":"","cgpa_tt":"","translated_pc":"","translated_ssn":"","class":"","cong_card":"","cong_port":"","cpc":"","xmt_lsn":"","rcv_lsn":"","sccp_mt":"","sio":"","alarms_alarm_num":"","stps_abbr":"","alarms_lsn":"","slc":"","card":"","port":"","linksets_company":"","opc":"","dpc":"","aftpc":"","feclli":"","cdpa_addr":"","cdpa_ssn":"","cdpa_tt":"","cgpa_addr":"","cgpa_ssn":"","cgpa_tt":"","translated_pc":"","translated_ssn":"","class":"","cong_card":"","cong_port":"","cpc":"","xmt_lsn":"","rcv_lsn":"","sccp_mt":"","sio":""}';

echo "\nDecode with Underscore in Name\n";
print_r( json_decode($string) );

echo json_last_error() ;

$string = '{"alarm_groups_id":"0","start":"2010-07-23 10:23:13","stop":"2010-07-23 12:23:13","model":"","pairs_id":"","alarmsalarmnum":"1044","stps_abbr":"dlls","alarmslsn":"bm700145","slc":"","card":"","port":"","linksets_company":"","opc":"","dpc":"","aftpc":"","feclli":"","cdpa_addr":"","cdpa_ssn":"","cdpa_tt":"","cgpa_addr":"","cgpa_ssn":"","cgpa_tt":"","translated_pc":"","translated_ssn":"","class":"","cong_card":"","cong_port":"","cpc":"","xmt_lsn":"","rcv_lsn":"","sccp_mt":"","sio":"","alarms_alarm_num":"","stps_abbr":"","alarms_lsn":"","slc":"","card":"","port":"","linksets_company":"","opc":"","dpc":"","aftpc":"","feclli":"","cdpa_addr":"","cdpa_ssn":"","cdpa_tt":"","cgpa_addr":"","cgpa_ssn":"","cgpa_tt":"","translated_pc":"","translated_ssn":"","class":"","cong_card":"","cong_port":"","cpc":"","xmt_lsn":"","rcv_lsn":"","sccp_mt":"","sio":""}';

echo "\nDecode without Underscore in Name\n";
print_r( json_decode($string) );

echo json_last_error() ;


?>

Expected result:
----------------
Decode with Underscore in Name
stdClass Object
(
    [alarm_groups_id] => 0
    [start] => 2010-07-23 10:23:13
    [stop] => 2010-07-23 12:23:13
    [model] => 
    [pairs_id] => 
    [alarms_alarm_num] => 1044
    [stps_abbr] => 
    [alarms_lsn] => bm700145
    [slc] => 
    [card] => 
    [port] => 
    [linksets_company] => 
    [opc] => 
    [dpc] => 
    [aftpc] => 
    [feclli] => 
    [cdpa_addr] => 
    [cdpa_ssn] => 
    [cdpa_tt] => 
    [cgpa_addr] => 
    [cgpa_ssn] => 
    [cgpa_tt] => 
    [translated_pc] => 
    [translated_ssn] => 
    [class] => 
    [cong_card] => 
    [cong_port] => 
    [cpc] => 
    [xmt_lsn] => 
    [rcv_lsn] => 
    [sccp_mt] => 
    [sio] => 
)
Decode without Underscore in Name
stdClass Object
(
    [alarm_groups_id] => 0
    [start] => 2010-07-23 10:23:13
    [stop] => 2010-07-23 12:23:13
    [model] => 
    [pairs_id] => 
    [alarmsalarmnum] => 1044
    [stps_abbr] => 
    [alarmslsn] => bm700145
    [slc] => 
    [card] => 
    [port] => 
    [linksets_company] => 
    [opc] => 
    [dpc] => 
    [aftpc] => 
    [feclli] => 
    [cdpa_addr] => 
    [cdpa_ssn] => 
    [cdpa_tt] => 
    [cgpa_addr] => 
    [cgpa_ssn] => 
    [cgpa_tt] => 
    [translated_pc] => 
    [translated_ssn] => 
    [class] => 
    [cong_card] => 
    [cong_port] => 
    [cpc] => 
    [xmt_lsn] => 
    [rcv_lsn] => 
    [sccp_mt] => 
    [sio] => 
    [alarms_alarm_num] => 
    [alarms_lsn] => 
)

Actual result:
--------------
Decode with Underscore in Name
stdClass Object
(
    [alarm_groups_id] => 0
    [start] => 2010-07-23 10:23:13
    [stop] => 2010-07-23 12:23:13
    [model] => 
    [pairs_id] => 
    [alarms_alarm_num] => 
    [stps_abbr] => 
    [alarms_lsn] => 
    [slc] => 
    [card] => 
    [port] => 
    [linksets_company] => 
    [opc] => 
    [dpc] => 
    [aftpc] => 
    [feclli] => 
    [cdpa_addr] => 
    [cdpa_ssn] => 
    [cdpa_tt] => 
    [cgpa_addr] => 
    [cgpa_ssn] => 
    [cgpa_tt] => 
    [translated_pc] => 
    [translated_ssn] => 
    [class] => 
    [cong_card] => 
    [cong_port] => 
    [cpc] => 
    [xmt_lsn] => 
    [rcv_lsn] => 
    [sccp_mt] => 
    [sio] => 
)
0
Decode without Underscore in Name
stdClass Object
(
    [alarm_groups_id] => 0
    [start] => 2010-07-23 10:23:13
    [stop] => 2010-07-23 12:23:13
    [model] => 
    [pairs_id] => 
    [alarmsalarmnum] => 1044
    [stps_abbr] => 
    [alarmslsn] => bm700145
    [slc] => 
    [card] => 
    [port] => 
    [linksets_company] => 
    [opc] => 
    [dpc] => 
    [aftpc] => 
    [feclli] => 
    [cdpa_addr] => 
    [cdpa_ssn] => 
    [cdpa_tt] => 
    [cgpa_addr] => 
    [cgpa_ssn] => 
    [cgpa_tt] => 
    [translated_pc] => 
    [translated_ssn] => 
    [class] => 
    [cong_card] => 
    [cong_port] => 
    [cpc] => 
    [xmt_lsn] => 
    [rcv_lsn] => 
    [sccp_mt] => 
    [sio] => 
    [alarms_alarm_num] => 
    [alarms_lsn] => 
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-23 23:36 UTC] php at calendee dot com
-Status: Open +Status: Closed
 [2010-07-23 23:36 UTC] php at calendee dot com
I've actually solved this "bug".  It's not a bug at all.  The problem was that my json string repeated the same names several times.  However, the values were not provided twice.  So, when I renamed the "alarms_alarm_num" and "alarms_lsn" names to "alarmsalarmnum" and "alarmslsn", I was actually correcting the fact that those names were listed twice.

Sorry for the bother.
 [2010-07-23 23:47 UTC] pajoye@php.net
-Status: Closed +Status: Bogus
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 02 17:01:35 2024 UTC