Invalid JSON parsing using PHP
- All the quotes should be double quotes
"
and not single quotes'
. - All the keys should be quoted.
- The whole element should be an object.
function my_json_decode($s) {
$s = str_replace(
array('"', "'"),
array('\"', '"'),
$s
);
$s = preg_replace('/(\w+):/i', '"\1":', $s);
return json_decode(sprintf('{%s}', $s));
}
How to convert partially invalid JSON to a valid one?
RegEx 1
Your original expression seems to be find, we would just slightly modify that to:
([{,])(\w+)(\s+)?:
and it might work, we are adding a left boundary:
([{,])
and a right boundary:
:
and our key attribute is in this capturing group:
(\w+)
RegEx 2
We can expand our first expression to:
([{,])(\s+)?(\w+)(\s+)?:
in case, we might be having spaces before the key attribute:
Demo
Test 1
$re = '/([{,])(\w+)(\s+)?:/m';
$x = '[{endTime:"2019-06-05T17:15:00.000+10:00",startTime:"2019-06-05T17:00:00.000+10:00"}]';
$subst = '$1"$2":';
$result = preg_replace($re, $subst, $x);
echo $result;
Test 2
$re = '/([{,])(\s+)?(\w+)(\s+)?:/m';
$x = '[{endTime:"2019-06-05T17:15:00.000+10:00",startTime:"2019-06-05T17:00:00.000+10:00"}]';
$subst = '$1"$3":';
$result = preg_replace($re, $subst, $x);
echo $result;
Output
[{"endTime":"2019-06-05T17:15:00.000+10:00","startTime":"2019-06-05T17:00:00.000+10:00"}]
Demo
RegEx Circuit
jex.im visualizes regular expressions:
Invalid JSON parsing using PHP
- All the quotes should be double quotes
"
and not single quotes'
. - All the keys should be quoted.
- The whole element should be an object.
function my_json_decode($s) {
$s = str_replace(
array('"', "'"),
array('\"', '"'),
$s
);
$s = preg_replace('/(\w+):/i', '"\1":', $s);
return json_decode(sprintf('{%s}', $s));
}
parsing invalid JSON in PHP
First, we will have to remove the IP address using the regex. Then You can use PHP and simply explode the string ` and get your JSON from the resulting array.
$var = '192.168.1.1 | SUCCESS => { "architecture": "amd64", "lsbdistcodename": "wheezy", "lsbdistdescription": "Debian GNU/Linux 7.9 (wheezy)", "lsbdistid": "Debian", "lsbdistrelease": "7.9", "lsbmajdistrelease": "7", "lsbminordistrelease": "9", "memoryfree": "653.05 MB", "memoryfree_mb": "653.05", "memorysize": "1002.94 MB", "memorysize_mb": "1002.94", "operatingsystem": "Debian", "operatingsystemmajrelease": "7", "operatingsystemrelease": "7.9", "os": { "family": "Debian", "lsb": { "distcodename": "wheezy", "distdescription": "Debian GNU/Linux 7.9 (wheezy)", "distid": "Debian", "distrelease": "7.9", "majdistrelease": "7", "minordistrelease": "9" }, "name": "Debian", "release": { "full": "7.9", "major": "7", "minor": "9" } }, "osfamily": "Debian", "partitions": { "sda1": { "filesystem": "LVM2_member", "size": "41938944" } }, "physicalprocessorcount": 1, "processor0": "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz", "processorcount": 1, "processors": { "count": 1, "models": [ "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz" ], "physicalcount": 1 }, "timezone": "EET", "uniqueid": "007f0101", "uptime": "360 days", "uptime_days": 360, "uptime_hours": 8644, "uptime_seconds": 31119057, "virtual": "vmware"}192.168.1.1 | SUCCESS => { "architecture": "x86_64", "is_virtual": true, "kernel": "Linux", "kernelmajversion": "3.10", "kernelrelease": "3.10.0-514.2.2.el7.x86_64", "kernelversion": "3.10.0", "macaddress": "00:50:56:b1:c6:49", "macaddress_eno16777984": "00:50:56:b1:c6:49", "memoryfree": "1.58 GB", "memoryfree_mb": "1617.09", "memorysize": "1.80 GB", "operatingsystemmajrelease": "7", "operatingsystemrelease": "7.3.1611", "os": { "family": "RedHat", "name": "CentOS", "release": { "full": "7.3.1611", "major": "7", "minor": "3" } }, "osfamily": "RedHat", "partitions": { "sda1": { "filesystem": "xfs", "mount": "/boot", "size": "1024000", "uuid": "e50a313f-98fa-4d7d-a40d-eb0c3b5f3ef5" }, "sda2": { "filesystem": "LVM2_member", "size": "30423040" } }, "path": "/usr/local/bin:/usr/bin", "physicalprocessorcount": 1, "processor0": "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz", "processorcount": 1, "processors": { "count": 1, "models": [ "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz" ], "physicalcount": 1 }, "timezone": "EET", "uniqueid": "44d40600", "uptime": "87 days", "uptime_days": 87, "uptime_hours": 2094, "uptime_seconds": 7539660, "virtual": "vmware"';
$pattern = '/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:\/\d{2})?/';
$replacement = '';
$result = preg_replace($pattern, $replacement, $var); // Remove the IP address
echo "$result".PHP_EOL;
$arr = explode("| SUCCESS =>", $result); //Explode the string by the constant string
print_r($arr);
Resulting Array
The first element will be empty.
Array
(
[0] =>
[1] => { "architecture": "amd64", "lsbdistcodename": "wheezy", "lsbdistdescription": "Debian GNU/Linux 7.9 (wheezy)", "lsbdistid": "Debian", "lsbdistrelease": "7.9", "lsbmajdistrelease": "7", "lsbminordistrelease": "9", "memoryfree": "653.05 MB", "memoryfree_mb": "653.05", "memorysize": "1002.94 MB", "memorysize_mb": "1002.94", "operatingsystem": "Debian", "operatingsystemmajrelease": "7", "operatingsystemrelease": "7.9", "os": { "family": "Debian", "lsb": { "distcodename": "wheezy", "distdescription": "Debian GNU/Linux 7.9 (wheezy)", "distid": "Debian", "distrelease": "7.9", "majdistrelease": "7", "minordistrelease": "9" }, "name": "Debian", "release": { "full": "7.9", "major": "7", "minor": "9" } }, "osfamily": "Debian", "partitions": { "sda1": { "filesystem": "LVM2_member", "size": "41938944" } }, "physicalprocessorcount": 1, "processor0": "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz", "processorcount": 1, "processors": { "count": 1, "models": [ "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz" ], "physicalcount": 1 }, "timezone": "EET", "uniqueid": "007f0101", "uptime": "360 days", "uptime_days": 360, "uptime_hours": 8644, "uptime_seconds": 31119057, "virtual": "vmware"}
[2] => { "architecture": "x86_64", "is_virtual": true, "kernel": "Linux", "kernelmajversion": "3.10", "kernelrelease": "3.10.0-514.2.2.el7.x86_64", "kernelversion": "3.10.0", "macaddress": "00:50:56:b1:c6:49", "macaddress_eno16777984": "00:50:56:b1:c6:49", "memoryfree": "1.58 GB", "memoryfree_mb": "1617.09", "memorysize": "1.80 GB", "operatingsystemmajrelease": "7", "operatingsystemrelease": "7.3.1611", "os": { "family": "RedHat", "name": "CentOS", "release": { "full": "7.3.1611", "major": "7", "minor": "3" } }, "osfamily": "RedHat", "partitions": { "sda1": { "filesystem": "xfs", "mount": "/boot", "size": "1024000", "uuid": "e50a313f-98fa-4d7d-a40d-eb0c3b5f3ef5" }, "sda2": { "filesystem": "LVM2_member", "size": "30423040" } }, "path": "/usr/local/bin:/usr/bin", "physicalprocessorcount": 1, "processor0": "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz", "processorcount": 1, "processors": { "count": 1, "models": [ "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz" ], "physicalcount": 1 }, "timezone": "EET", "uniqueid": "44d40600", "uptime": "87 days", "uptime_days": 87, "uptime_hours": 2094, "uptime_seconds": 7539660, "virtual": "vmware"
)
Note : This regex is for IPV4 addresses that I got from IPV4 regex. You can change the regex for removing the IPV6 addresses too.
Working IDEONE Link
Invalid JSON data PHP
Your issue is that $_POST
is an array, you actually want to do
json_decode($_POST[0]);
In order to decode only the first object in your post array.
However, you'll quickly notice that this won't work either; the $_POST
array expects you to pass in a key-value json object, or an array. In your case, you do pass through a json object, but it becomes weirdly formatted because you do not pass through a key. I'm not entirely certain about this, but by the looks of your var_dump
ed array, I'm quite certain the issue is that you do not pass it in an array. I also would like to point out that if you set the correct headers and just pass the object, the backend should be able to read it, just as easily as it reads form-data
Related Topics
Destroy Session When Broswer Tab Closed
How to Dynamically Populate Options on Dropdown Lists Based on Selection in Another Dropdown
Difference Between Var_Dump,Var_Export & Print_R
Phpmyadmin Automatic Logout Time
Phpmailer: Reply Using Only "Reply To" Address
How to Parse a CSV File to Grab the Column Names First Then the Rows That Relate to It
Remotely Destroy a Session in PHP (User Logs in Somewhere Else)
Get Timestamp of Today and Yesterday in PHP
Woocommerce: Set Country by Default in Checkout Page
How to Do Multiple SQL Statements in One MySQL_Query
Check If Url Has Certain String with PHP
Issues with PHP 5.3 and Sessions Folder
How to Alias the Name of a Column in Eloquent