0

What is wrong with this JSON string in PHP?

[{"Type":"Chasse|Loisirs","Productions":"Bois d\'Œuvre|Bois de chauffage","Essences principales > Feuillus":"Bouleaux|Hêtres|Merisiers|Peupliers"}]

I try online tools validators like this one, and JSON seems valid, but with PHP I still have an error:

$result = json_decode($json) 
// JSON_ERROR_SYNTAX

I try remove UTF-8 BOM, stripslashes, htmlentities... without success.

Why this JSON is malformed, and how to make it OK?

9
  • Nope its working. Online Check Commented May 16, 2016 at 7:37
  • 2
    \' is not valid JSON. Commented May 16, 2016 at 7:38
  • 1
    @Bertrand Because the JSON is invalid? Once again, \' is not valid JSON. Commented May 16, 2016 at 7:49
  • 2
    @aldrin27: No, it isn't, and jsonlint.com helpfully says so. :-) Commented May 16, 2016 at 7:53
  • 1
    @FrayneKonok: The JSON is not valid. Your check is only working because the JSON is inside a PHP string quoted with ', so the \' is handled by PHP and just a ' ends up in the JSON. But JSON with an actual \' sequence in a string is not valid. Commented May 16, 2016 at 7:55

2 Answers 2

4

The JSON is not valid because you do NOT need to escape ' character. Check for example this question How to escape special characters in building a JSON string?

The JSON RFC 7159 has this section

  char = unescaped /
      escape (
          %x22 /          ; "    quotation mark  U+0022
          %x5C /          ; \    reverse solidus U+005C
          %x2F /          ; /    solidus         U+002F
          %x62 /          ; b    backspace       U+0008
          %x66 /          ; f    form feed       U+000C
          %x6E /          ; n    line feed       U+000A
          %x72 /          ; r    carriage return U+000D
          %x74 /          ; t    tab             U+0009
          %x75 4HEXDIG )  ; uXXXX                U+XXXX

The ' is not in the list.

I made a lot of changes with the question related with escaping Unicode characters. Formally this is not required. Check for example this question JSON and escaping characters

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

1 Comment

Finally done with editing. Hope now the answer is correct. Please let me know if you see any issues.
0

Victor Smirnov already gave a good answer above, however I want to explain how you can debug such an error:

If you enter your JSON in http://jsonlint.com/ it will show the following:

Error: Parse error on line 3:
...s",  "Productions": "Bois d\'Œuvre|Bois 
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

This does not immediately show you what is wrong exactly, but it does show you that something is wrong with the string

"Bois d\'Œuvre|Bois de chauffage"

By replacing or removing the string character by character, you can find out that the single quote should not be escaped.

So it should be:

"Bois d'Œuvre|Bois de chauffage"

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.