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"
        }
6
  • 4
    Can you fix the thing that gives you that invalid output? Commented Sep 12, 2016 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 Commented Sep 12, 2016 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" } Commented Sep 12, 2016 at 12:39
  • can you please help me with that ? Commented Sep 12, 2016 at 12:40
  • I have found this from a website html code Commented Sep 12, 2016 at 12:43

1 Answer 1

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"
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.