9

I'm trying to write a php script that handles data from a webservice that delivers "json" as a string. The problem is the string isn't really json; it's javascript. Specifically, the keys are not quoted, although the variables are. Example (the actual data is much longer and more complicated):

{desc:'User defined payload'}

As described by the php manual, json_decode() correctly fails to interpret this string.

My question is, how can I successfully interpret a string like this in php?

The only solution I can think of is to write some regular expressions that fix the syntax, but then I'd have two problems.

EDIT

Hadvig's suggestion of using the Services_JSON pear module worked, and looks like a general solution. Once I had the module installed, my code looked like this:

require_once 'PEAR.php';
require_once 'Services/JSON.php';

$Services_JSON = new Services_JSON();
$data = $Services_JSON->decode($malformed_json);

Unfortunately, this is SLOW. To interpret the whole string (~400,000 chars) took > 36 seconds! Using a regular expression to fix the quotes and then using json_decode took ~0.04 seconds. Here's what I used:

// fix single quotes
$s = str_replace("'", '"', $malformed_json);

// fix unquoted keys
$valid_json = preg_replace('/([{\[,])\s*([a-zA-Z0-9_]+?):/', '$1"$2":', $s);

$data = json_decode($valid_json);

Of course, this will break if the data contains any quotes, brackets, or commas.

4
  • Could you alter whatever process is creating the string? Commented Jul 26, 2011 at 21:05
  • If you know the two-problems quote, then you probably know the quote about using regular expressions when you should be using a parser ;) Commented Jul 26, 2011 at 21:10
  • Joe: I would love to know about a parser that could handle this. Can you point me to one? Commented Jul 26, 2011 at 21:16
  • Bug the people who can fix the format! Commented Jul 26, 2011 at 22:48

5 Answers 5

2

Ok. try to use this. http://pear.php.net/pepr/pepr-proposal-show.php?id=198 I just check your string

Sign up to request clarification or add additional context in comments.

Comments

1

Depends on how complicated your data is :

$output = "{desc:'User defined payload',asc:'whatever'}";

function json_js_php($string){

    $string = str_replace("{",'{"',$string);
    $string = str_replace(":'",'":"',$string);
    $string = str_replace("',",'","',$string);
    $string = str_replace("'}",'"}',$string);
    return $string;

}

echo json_decode(json_js_php($output))->{'desc'}; 

returns : User defined payload

Comments

1

Using regexp is a no-go. JSON grammar cannot be correctly parsed using regexp. You will open yourself to a ton of future bugs.

I recommend using some kind of a YAML parser. YAML is backwards-compatible with JSON and allows unquoted literals at the same time.

Symfony YAML component worked great for me.

And remember that there will be a performance penalty in comparison to json_decode because it's implemented natively.

Comments

0

If the problem is just the unquoted identifiers and the data can be assumed not to contain any curly brackets, this should do it:

$goodJson = preg_replace("/{\s*([a-zA-Z0-9_]+)/", '{ "$1"', $badJson);

(not tested!)

Comments

-1

Try this:

$jsonString = "{result:true,username:'usr000242',password:'123456',message:'Cannot send username and password to [email protected]'}";
function manualFixInvalidJSON($jsonString=''){
    $jsonString = preg_replace("/([{,])([a-zA-Z][^: ]+):/", "\$1\"$2\":", $jsonString);
    $jsonString = preg_replace("/:([a-zA-Z\'][^:]+)([,}])/", ":\"$1\"$2", $jsonString);
    $jsonString = json_decode($jsonString,true);
    function trimer($val){
        return trim(trim($val,"'"),"\"");
    }
    $jsonString = array_map('trimer', $jsonString);
    return json_encode($jsonString);
}
echo jsonString($jsonString);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.