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?

Biffen
  • 6,249
  • 6
  • 28
  • 36
Bertrand
  • 1,000
  • 1
  • 9
  • 20

2 Answers2

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

Community
  • 1
  • 1
Victor Smirnov
  • 3,450
  • 4
  • 30
  • 49
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"
vrijdenker
  • 1,371
  • 1
  • 12
  • 25