0

how can I convert following invalid json string to valid in php I need solution in php. Please give me a way to convert this input json to output json

input:

{
            account : 'rogersrmisports',
            brand : 'Sportsnet',
            tags : '',
            cmsid : '2912963',
            wordCount : '3197',
            publishDate : 'July 11, 2016',
            products : '',
            pages : 'sportsnet : hockey : nhl : david amber q&a, part i: \'my job is not to be ron jr.\'',
            section : 'sportsnet : hockey',
            subSection : 'sportsnet : hockey : nhl',
            contentName : 'david amber q&a, part i: \'my job is not to be ron jr.\'',
            complete90percent : false, // default: false
        }

output:

{
            "account" : "rogersrmisports",
            "brand" : "Sportsnet",
            "tags" : "",
            "cmsid" : "2912963",
            "wordCount" : "3197",
            "publishDate" : "July 11, 2016",
            "products" : "",
            "pages" : "sportsnet : hockey : nhl : david amber q&a, part i: 'my job is not to be ron jr.'",
            "section" : "sportsnet : hockey",
            "subSection" : "sportsnet : hockey : nhl",
            "contentName" : "david amber q&a, part i: 'my job is not to be ron jr.'",
            "complete90percent" : "false, // default: false"
        }
Progi1990
  • 57
  • 1
  • 3
  • 13
  • 4
    Can you fix the thing that gives you that invalid output? – Jonnix Sep 12 '16 at 12:33
  • 2
    from where you get this invalid input?may be there is a better way to fix this.can you tell more details – Madhawa Priyashantha Sep 12 '16 at 12:37
  • sorry i need this output : { "account": "rogersrmisports", "brand": "Sportsnet", "tags": "", "cmsid": "2912963", "wordCount": "3197", "publishDate": "July 11, 2016", "products": "", "pages": "sportsnet : hockey : nhl : david amber q&a, part i: 'my job is not to be ron jr.'", "section": "sportsnet : hockey", "subSection": "sportsnet : hockey : nhl", "contentName": "david amber q&a, part i: 'my job is not to be ron jr.'", "complete90percent": "false, // default: false" } – Progi1990 Sep 12 '16 at 12:39
  • can you please help me with that ? – Progi1990 Sep 12 '16 at 12:40
  • I have found this from a website html code – Progi1990 Sep 12 '16 at 12:43
  • Maybe this question helps: http://stackoverflow.com/questions/8815586/convert-invalid-json-into-valid-json – Roman Hocke Sep 13 '16 at 08:30

1 Answers1

1

You can use this. Please keep in mind that this eventually works just for your specified scenario. Other input can lead to problems. You have to check this yourself.

// use regex to get data
$matches = [];
preg_match_all('/([A-Za-z0-9]*?)\s:\s(?:(?:\'(.*)\')|(.*)),/', $str, $matches);

// combine arrays of matches, remove slashes
array_walk($matches[2], function(&$val, $key) use ($matches) {
    if (empty($val)) {
        $val = $matches[3][$key];
    }
    $val = str_replace("\'", "'", $val);
});

// create data array
$result = array_combine($matches[1], $matches[2]);

// convert data array to json
$json = json_encode($result);


print_r($json);

Output:

{  
   "account":"rogersrmisports",
   "brand":"Sportsnet",
   "tags":"",
   "cmsid":"2912963",
   "wordCount":"3197",
   "publishDate":"July 11, 2016",
   "products":"",
   "pages":"sportsnet : hockey : nhl : david amber q&a, part i: 'my job is not to be ron jr.'",
   "section":"sportsnet : hockey",
   "subSection":"sportsnet : hockey : nhl",
   "contentName":"david amber q&a, part i: 'my job is not to be ron jr.'",
   "complete90percent":"false"
}
Philipp Palmtag
  • 1,310
  • 2
  • 16
  • 18