valid_json() has problems with escaped characters in query() strings
jeremy-j-ackso opened this issue · 3 comments
I'm trying to query for some JSON that's embedded in an ES document as a string. To do that I need to include the quotes internal to that JSON in my regexp search.
A couple very simplified examples of documents I'm searching through for matched text:
{message:'{"product":"toothbrush"}'}
{message:'{"product":"hairspray"}'}
{message:'{"product":"ear swabs"}'}
Here's the query I'm trying to wrap in query()
:
qry <- '{
"regexp": {
"message":"^\\{.*?\\"product\\":\\"(toothbrush|hairspray)\\".*?\\}$"
}
}'
And here's what I'm getting when I look at it using query()
, cat()
, and valid_json()
:
> query(qry)
Error in query(qry) : valid_json(json) is not TRUE
> cat(qry)
{
"regexp": {
"message":"^\{.*?\"product\":\"(toothbrush|hairspray)\".*?\}$"
}
}
> elasticsearchr:::valid_json(qry)
[1] FALSE
attr(,"offset")
[1] 29
attr(,"err")
[1] "lexical error: inside a string, '\\' occurs before a character which it may not.\n { \"regexp\": { \"message\":\"^\\{.*?\\\"product\\\":\\\"(toothbrush|\n (right here) ------^\n"
I need to be able to escape the internal "
, {
, and }
because these are reserved characters according to the docs and I need to a) treat them literally, and b) not close the string they are contained within early.
At the risk of stating the obvious, all JSON validation is handled entirely by the validate
function from the jsonlite package. I think your best bet is to take a look over there for potential solutions.
Ah, I had not taken notice of that. Didn't dig too deeply. Thanks for the pointer!
Sorry I couldn't be more helpful. The jsonlite package is well documented and supported so I reckon you stand a good chance of finding a solution.