DrDice DrDice - 19 days ago 5
JSON Question

php json decode weird format

I'm trying to decode some data from my IP alarm system. I have been told that the data is json, and looking through the html files, there are references to json. The problem is that it looks a little wierd :-)

Here are the raw output from the alarm:

/*-secure- { senrows : [ {no : "1", type : "Door Contact", zone : "1", name : "zone1", attr : "Burglar", cond : "", battery : "", tamp : "", bypass : "No"}, {no : "2", type : "Door Contact", zone : "2", name : "zone2", attr : "Burglar", cond : "", battery : "", tamp : "", bypass : "No"}, {no : "3", type : "Door Contact", zone : "3", name : "zone3", attr : "Burglar", cond : "", battery : "", tamp : "", bypass : "No"}, {no : "4", type : "Door Contact", zone : "4", name : "zone4", attr : "Burglar", cond : "", battery : "Low", tamp : "", bypass : "No"}, {no : "5", type : "Door Contact", zone : "5", name : "zone5", attr : "Burglar", cond : "", battery : "", tamp : "", bypass : "No"}, {no : "6", type : "Door Contact", zone : "6", name : "zone6", attr : "Burglar", cond : "", battery : "", tamp : "", bypass : "No"}, {no : "7", type : "Door Contact", zone : "7", name : "zone7", attr : "Burglar", cond : "", battery : "", tamp : "", bypass : "No"}, {no : "8", type : "Door Contact", zone : "8", name : "zone8", attr : "Burglar", cond : "", battery : "", tamp : "", bypass : "No"}, {no : "9", type : "Door Contact", zone : "9", name : "zone9", attr : "Burglar", cond : "", battery : "", tamp : "", bypass : "No"}, {no : "10", type : "IR Sensor", zone : "10", name : "zone10", attr : "Burglar", cond : "", battery : "Low", tamp : "", bypass : "No"}, {no : "11", type : "Remote Keypad", zone : "11", name : "zone11", attr : "",cond : "", battery : "", tamp : "", bypass : "No"}] } */


how to decode this data?? :-)

json_decode() does not work :-/

Answer

well, you still should first and foremost demand from whoever gave you this output that they fix their code and actually deliver JSON.

still, i had a few minutes and enjoyed the coding exercise to write a quick fix for your data.
it only works if your code allways starts with the same -secure-, otherwise you have to adapt it.

and it will fail or produce weird results if for some reason you have a field with encapsulated strings, like a:"text\"moretext\"finaltext", or if the providers introduce any other mistake besides the ones shown in your example.

<?php
$data = /*your not-JSON-string*/;

function fix_botched_json($data) {
    //remove comment-section, we don't need it
    $data = substr($data, 10, strlen($data)-12);
    $seek = '/(\w+)/';
    $exploded = explode('"', $data);
    //explode so we only replace *outside* of strings

    $outside = true;
    foreach($exploded as &$item) {
        if($outside) {
            //the regex matches only word characters.
            $item = preg_replace($seek, '"\1"', $item);
        }
        //alternating between inside and outside of strings
        $outside = !$outside;
    }
    //piece it back together
    $imploded = implode('"', $exploded);

    return $imploded;
}


$final = json_decode(fix_botched_json($data), true);