3

Versions: Neo4j 3.2.2 Community; APOC 3.2.0.3

Escaped characters in strings are not handled correctly in all instances.

This works as expected:

WITH apoc.convert.fromJsonMap( '{"a":42,"b":"foo\\bar","c":[1,2,3]}') AS p RETURN p.b

╒═════════╕
│"p.b" │
╞═════════╡
│"foo\bar"│
└─────────┘

Escaping quotes does not work as expected:

WITH apoc.convert.fromJsonMap( '{"a":42,"b":"\"foo\"","c":[1,2,3]}')
AS p RETURN p.b

Neo.ClientError.Procedure.ProcedureCallFailed Failed to invoke function apoc.convert.fromJsonMap: Caused by: java.lang.RuntimeException: Can't convert {"a":42,"b":""foo"","c":[1,2,3]} to Map with path

Doubling down on the reverse solidus (not desirable) gives this result:

WITH apoc.convert.fromJsonMap( '{"a":42,"b":"\\"foo\\"","c":[1,2,3]}') AS p RETURN p.b

╒═════════╕
│"p.b" │
╞═════════╡
│"\"foo\""│
└─────────┘

An escaped newline character gets the same error:

WITH apoc.convert.fromJsonMap( '{"a":42,"b":"foo\nbar","c":[1,2,3]}') AS p RETURN p.b

Neo.ClientError.Procedure.ProcedureCallFailed Failed to invoke function apoc.convert.fromJsonMap: Caused by: java.lang.RuntimeException: Can't convert {"a":42,"b":"foo bar","c":[1,2,3]} to Map with path

[Note that the newline after "foo" is represented in the string returned by the error message. The error message is given on two lines.]

Is my usage of this procedure correct?

Has anyone seen this problem and fixed or worked around it?

cybersam
  • 63,203
  • 6
  • 53
  • 76
jimwilli
  • 31
  • 2

1 Answers1

0

Using a double-backslash in place of a single backslash to escape characters actually works.

For example, these 2 queries both return true:

RETURN apoc.convert.fromJsonMap( '{"a":42,"b":"\\"foo\\"","c":[1,2,3]}').b = '"foo"';

RETURN apoc.convert.fromJsonMap( '{"a":42,"b":"foo\\nbar","c":[1,2,3]}').b = 'foo\nbar';
cybersam
  • 63,203
  • 6
  • 53
  • 76
  • 1
    This is a good observation. However, the JSON input string is not really valid JSON. I'll keep this in mind for a potential workaround, though. – jimwilli Jul 17 '17 at 12:37