0

I receive this JSON data (generated not by me) on my server:

{"publicKey": "D2Lxs7gD3cK2Yc+YxJL2iw\dawww1"}

When i try to json_decode it, the result is NULL because of the backslash the string contains. I can't control the string's contents, since i receive it externally.

I tried using addslashes and json_encode before i try to decode it, but to no avail.

How can i successfully decode this JSON string?

Semion Vino
  • 121
  • 1
  • 9
  • Can you use json_last_error() to get the error information an add this information to you question? – Pedro Jul 13 '22 at 15:09
  • Duplicate of: https://stackoverflow.com/questions/43769456/json-parsing-error-with-backslash and/or https://stackoverflow.com/questions/3807537/why-does-the-jquery-json-parser-need-double-escaping-for-backslashes - good question, but answered so VTC – TCooper Jul 13 '22 at 15:15
  • or, php specific version of the question: https://stackoverflow.com/questions/32056940/how-to-deal-with-backslashes-in-json-strings-php – TCooper Jul 13 '22 at 15:18
  • 2
    This simply is not (valid) JSON, so you should really complain to the generator of that data, so that they fix the bug in their code. – Ulrich Eckhardt Jul 13 '22 at 15:28
  • For completeness, the _valid_ version would simply be: `{"publicKey": "D2Lxs7gD3cK2Yc+YxJL2iw\\dawww1"}` with a properly escaped backslash. Whoever is producing that data is doing it wrong, and _that_ is what needs to be fixed. – Sammitch Jul 13 '22 at 18:21

1 Answers1

-1

Try escaping these \ with additional \:

$json = str_replace('\\', '\\\\', $json);

Example

Justinas
  • 41,402
  • 5
  • 66
  • 96