user7190476
user7190476

Reputation: 35

parsing invalid JSON in PHP

I have this little problem. I have this linux machine outputting this kind of JSON:

xxx.xx.x.x | SUCCESS => {
    "architecture": "amd64", 
    "blockdevice_fd0_size": 0, 
    "blockdevice_sda_model": "Virtual disk", 
    "blockdevice_sda_size": 21474836480, 
    "blockdevice_sda_vendor": "VMware", 
    "blockdevice_sr0_model": "VMware IDE CDR10", 
... etc ... etc...
}

As you can see, its not valid... I would like to parse this file as a json. I know I somehow have to get rid of these xxx.xx.x.x | SUCCESS=> -lines.Regexp` maybe? There are many entries in file. I tried strstr with for loop, some regexp and so. If someone could give me a pointer :). Thanks in advance.

Upvotes: 0

Views: 100

Answers (3)

Ayush
Ayush

Reputation: 759

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

Upvotes: 1

blurstream
blurstream

Reputation: 467

You could use a regular expression too:

// create the input json
$my_json = '212.68.0.4 | SUCCESS => { "architecture": "amd64", "blockdevice_fd0_size": 0, "blockdevice_sda_model": "Virtual disk", "blockdevice_sda_size": 21474836480, "blockdevice_sda_vendor": "VMware", "blockdevice_sr0_model": "VMware IDE CDR10"}';

// search for all chars before "{" and replace it with the bracket ({) using \\1
$output_json = preg_replace("/.*({)/", "\\1", $my_json);

// show the output result
echo $output_json;

Upvotes: 0

Sebastian Berge
Sebastian Berge

Reputation: 86

Edit: Ayush got a much prettier way

Will it be possible to jump to the first '{', and then just remove the contents before?

$findme   = '{';
$pos = strpos($json, $findme);

And then

$json= substr($json, $pos, 0);

Upvotes: 0

Related Questions