Tzury Bar Yochay
Tzury Bar Yochay

Reputation: 9004

Mongo Insert Error - InvalidDocument - Key names must not contain the NULL byte

When attempting to insert the following JSON document:

{
  "Cmd": "Sync",
  "DeviceId": "ApplF4KLR7XXXXXX",
  "DeviceType": "iPad",
  "User": "jackXXXXX",
  "\u0003\u0001j\u0000E\\OK\u00031102699800\u0000\u0001R\u000326\u0000\u0001S\u00030\u0000\u0001WX\u00033\u0000\u0001b\u00032\u0000\u0001\u0000\u0011EF\u00034\u0000\u0001G\u000332768\u0000\u0001\u0001\u0001\u0000\u0000VJM\u000326:4909\u0000\u0001\u0001\u0001\u0001\u0001\u0001": true
}

into a mongo collection - I get the error " InvalidDocument - Key names must not contain the NULL byte".

The structure is representing a log line generated while logging MS ActiveSync POST requests towards OWA server.

Question is: What is the safest way to escape this special char and yet keep the new structure / value as close as possible to the original.

Upvotes: 0

Views: 3702

Answers (1)

Trudbert
Trudbert

Reputation: 3198

The problem is the last field key which contains a lot of unicode characters (include the null byte \u0000):

\u0003\u0001j\u0000E\\OK\u00031102699800\u0000\u0001R\u000326\u0000\u0001S\u00030\u0000\u0001WX\u00033\u0000\u0001b\u00032\u0000\u0001\u0000\u0011EF\u00034\u0000\u0001G\u000332768\u0000\u0001\u0001\u0001\u0000\u0000VJM\u000326:4909\u0000\u0001\u0001\u0001\u0001\u0001\u0001

The question is do you want to keep the unicode in the field key? If not you could try something like

.decode('unicode_escape').encode('ascii','ignore'))

taken from this answer.

Upvotes: 1

Related Questions