1

This string

{\x22Address\x22:\x22some address with quotes \x22}

is parsed by JSON.parse correctly in browser. Why? What do hex numbers mean in json string? I can't find explanation.

Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
mtkachenko
  • 5,389
  • 9
  • 38
  • 68

3 Answers3

7

In Javascript a backslash is an escape character. There are several escape sequences, you can find a list here.

The most important:

  • \x followed by two hexadecimal characters represent a character by it's ascii code
  • \u followed by four hexadecimal characters represent a character by it's unicode number
  • \t, \r, \n you certainly know already. They are tab, carriage return and new line respectively.
Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
  • 1
    Important note! This is a property of JavaScript strings, *not JSON strings*. Ref [json.org](https://www.json.org/) and section 9 of the [ECMA standard](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf). – Brendan Jul 22 '19 at 06:32
2

If you look up the hex value 22 in a ascii table, you can see that its the quote sign ( " ). Thats why its parsed correctly. http://www.asciitable.com/

var str= "{\x22test\x22: \x22hello\x22}";
var test = JSON.parse(str);
console.dir(test);

{ test: 'hello' }

eirik
  • 101
  • 2
  • 8
1

Try

console.log(decodeURIComponent("\x22")); // `"`

See ascii Chart

Cheeso
  • 189,189
  • 101
  • 473
  • 713
guest271314
  • 1
  • 15
  • 104
  • 177