简体   繁体   中英

Convert invalid json into valid json in php

I am saving some values in database via api(So I can not modify it manually)

When I get back it from database, the json value is not in valid form. I do not want to modify each value in database. I want something in php which can convert that into valid format.

Something like this in my db:

$invalid='{
    "response": {

                "id": "16"",  <--------------------- Invalid Format(It can be for any key)
                "event_name": "testing",
                "image": "images/Penguins.jpg",
                "event_date": "2014-12-13",
                "event_time": "02:10",
                "time_interval": "4",
                "location": "mohali",
                "event_type": "Rock",
                "detail": "sfdsf fgf ghb\t",
                "delivery": "dggh fghgfh\t\t",
                "status": "1"
            }
           }';

So how can I convert it in valid json by php like this:

$invalid='{
    "response": {

                "id": "16",  <--------------------- valid Format
                "event_name": "testing",
                "image": "images/Penguins.jpg",
                "event_date": "2014-12-13",
                "event_time": "02:10",
                "time_interval": "4",
                "location": "mohali",
                "event_type": "Rock",
                "detail": "sfdsf fgf ghb\t",
                "delivery": "dggh fghgfh\t\t",
                "status": "1"
            }
           }';

For this exact case, this will do:

<?php
$invalid='{
  "response": {        
    "id": "16"", 
    "event_name": "testing",
    "image": "images/Penguins.jpg",
    "event_date": "2014-12-13",
    "event_time": "02:10",
    "time_interval": "4",
    "location": "",
    "event_type": "Rock"",
    "detail": "sfdsf fgf ghb\t",
    "delivery": "dggh fghgfh\t\t",
    "status": "1""
  },
  "bar":{ "test": ""
  }
}';
$invalid = explode("\n",$invalid);
foreach($invalid as $idx => &$line) {
  $num = 0;
  for($z=0;$z<strlen($line);++$z) {
    $ch = $line[$z];
    if($ch == "\\") {
      ++$z;
    } else if($ch == '"') {
      ++$num;
      $last_pos = $z;
    }
  }
  if($num % 2 == 1) {
    $line[$last_pos] = ' ';
  }             
}       
$valid = implode("\n",$invalid);
print_r($valid);

Output:

{
  "response": {        
    "id": "16" , 
    "event_name": "testing",
    "image": "images/Penguins.jpg",
    "event_date": "2014-12-13",
    "event_time": "02:10",
    "time_interval": "4",
    "location": "",
    "event_type": "Rock" ,
    "detail": "sfdsf fgf ghb\t",
    "delivery": "dggh fghgfh\t\t",
    "status": "1" 
  },
  "bar":{ "test": ""
  }
}

But it's really recommended to fix the data on the database and the form/code that produce those invalid JSON ^^

In what ways can it be invalid? If it's only the existence of double quotes just use preg_replace:

$valid = preg_replace('/""/','"',$invalid);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM