2

In the code below, the JSON in the $datastring variable passes as valid JSON on JSONLint.

However, I get Syntax error when using PHP's json_encode.

What can I do to fix this? I've tried stripping the slashes but get same error.

$datastring = '[{"Program": 3034370,"Column": "CLI_RID","Value": "1006278"},{"Program": 3034370,"Column": "Filename","Value": "\\\\henery\\1006278\\CONFIRMATION LETTER AVAILABLE.html"},{"Program": 3034370,"Column": "EVENT_CODE","Value": "20120725ZZAQ"},{"Program": 3034370,"Column": "DOC_NAME","Value": "Confirmation Email"},{"Program": 3034370,"Column": "PrintDate","Value": "20120410"},{"Program": 2959623,"Column": "ISSUE","Value": "Res Spring"},{"Program": 2959623,"Column": "FILENAME","Value": "~/Res/Mag.aspx?I=res_spring&P=1006278"}]';
$data = json_decode($datastring, true);

// Define the errors.
$json_errors = array(
    JSON_ERROR_NONE => 'No error has occurred',
    JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
    JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
    JSON_ERROR_SYNTAX => 'Syntax error',
);

if(!$data) {
echo 'Last error : '. $json_errors[json_last_error()]. PHP_EOL. PHP_EOL;
} else {
echo print_r($data, true);
}

I will be getting this JSON string from an ASP.Net page.

2 Answers 2

3

If you write out a string in a php literal constant, you'll have to double all the slashes. So when the original JSON is something like:

{"foo": "backslash: \\"}

The corresponding php string literal is:

$json = '{"foo": "backslash: \\\\"}';
Sign up to request clarification or add additional context in comments.

2 Comments

The backslashes are ALREADY doubled.
No they're not. The code contains the php literal '"\\\\henery\\1006278\\CONFIRMATION LETTER AVAILABLE.html"', which is the string "\\henery\1006278\CONFIRMATION LETTER AVAILABLE.html". In JSON, backslashes in strings must be escaped, and \1 is an invalid literal.
0

You need more backslashes, not fewer; PHP strips one level itself since you're using it in a string literal.

4 Comments

@MB34: IT NEEDS TO BE QUADRUPLED.
Can't do that with addslashes because it will add them to the quotes as well.
The string is going to be coming from ASP, as I noted above.
Then the code in the question is inaccurate, since it shows it in a string literal.

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.