-1

Is there anyway to do this in JavaScript:

$ cat test.json 
{"body":"\u0000"}

$ python3 -c 'import json; print(json.load(open("test.json", "r")))'
{'body': '\x00'}

Notice, the data above only one \ (does not need to be escaped). So you have the following situation in JavaScript:

JSON.parse('{"body":"\\u0000"}') // works
JSON.parse('{"body":"\u0000"}') // does not work

With potentially any UTF-8 data comming from a binary source (websocket), can this data be processed directly like in the first python example above?

jcalfee314
  • 4,642
  • 8
  • 43
  • 75
  • In Javascript, like in Python, ``\`` has special meaning when defining a string. You have to double it to ``\\`` to remove than special meaning. The *resulting string value* will really only have one backslash, followed by the character `u`, etc. – Martijn Pieters Sep 01 '16 at 20:55
  • In the `test.json` file you are not defining a Python or Javascript string literal, so no, there are no doubled backslashes there. – Martijn Pieters Sep 01 '16 at 20:56
  • 1
    In ECMAscript 6 there is a [`String.raw` syntax](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/raw) that could give you what you want: ``JSON.parse(String.raw`{"body":"\u0000"}`)`` – Martijn Pieters Sep 01 '16 at 21:06
  • Last but not least, data coming in from a websocket is data and not a string literal, so backslashes won't be escaped. – Martijn Pieters Sep 01 '16 at 21:23

1 Answers1

0

String characters from \u0000 through \u001F are considered as control characters, and according to RFC-7159 are not allowed characters to use in JSON and must be escaped, as stated in section 7.

What you are trying to do is to put unescaped control characters into a JSON, which clearly not acceptable, you have to escape it first, non of the languages accept it, even Python.

The correct answer would be place a UTF-8 encoded value into a string containing a JSON format.

This is a correct JSON, and will be parsed by any JSON parser in any language, even in JavaScript:

{"body":"\u0000"}

This is incorrect JSON (consider the [NUL] as a NUL control character, as it cannot be represented in text):

{"body":"[NUL]"}

That's why JSON.parse('{"body":"\\u0000"}') works and JSON.parse('{"body":"\u0000"}') doesn't.

Hope, it clarifies what's wrong with your test.

Slavik Meltser
  • 9,712
  • 3
  • 47
  • 48