Json parse exception

Hi Team,
We are using elastic search cloud. we got below error. Please help me in resolving this.

PHP Fatal error: Uncaught Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"json_parse_exception","reason":"Unrecognized character escape '0' (code 48)
at [Source: (org.elasticsearch.common.bytes.AbstractBytesReference$MarkSupportingStreamInputWrapper); line: 7, column: 76]"}],"type":"json_parse_exception","reason":"Unrecognized character escape '0' (code 48)
at [Source: (org.elasticsearch.common.bytes.AbstractBytesReference$MarkSupportingStreamInputWrapper); line: 7, column: 76]"},"status":400} in D:\home\site\wwwroot\elastic-search\elastic\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php:632
Stack trace:
#0 D:\home\site\wwwroot\elastic-search\elastic\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php(317): Elasticsearch\Connections\Connection->process4xxError(Array, Array, Array)
#1 D:\home\site\wwwroot\elastic-search\elastic\vendor\react\promise\src\FulfilledPromise.php(28): Elasticsearch\Connections\Connection->Elasticse in D:\home\site\wwwroot\elastic-search\elastic\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php on line 632

we are using below code for before passing a value to query string, Here we taken a string value given by users.

$string = the input is given by users

$str = htmlentities($string, ENT_COMPAT, "UTF-8");

$str = preg_replace('/&([a-zA-Z])(uml|acute|grave|circ|tilde|laquo|shy|not|ordf|raquo|cedil|lig);/','$1',$str);
$string = html_entity_decode($str);
$string = form_safe_json($string);
$searchstringmsg = escapeElasticReservedChars($string);

function escapeElasticReservedChars($string) {
// echo $string;

$regex = "/[\+\#\$\%\@\_\-\=\&\|\!\(\)\{\}\[\]\^\"\~\*\<\>\?\:\\\/]/";
$str = preg_replace($regex, addslashes('\$0'), $string);

return addslashes($str);
}

function form_safe_json($json) {
$json = empty($json) ? '' : $json ;
$search = array('\',"\n","\r","\f","\t","\b","'") ;
$replace = array('\\',"\n", "\r","\f","\t","\b", "&#039");
$json = str_replace($search,$replace,$json);
return $json;
}

{
"query": {
"multi_match": {
"query": "' . $searchstringmsg . '",
"type": "most_fields",
"fields": [
"post_title",
"post_content",
"custom.cat_name",
"custom.series_name",
"custom.topic_name",
"custom.genre_name",
"post_author.display_name",
"post_meta.wps_subtitle",
"terms.post_tag.name"
],
"fuzziness": "1"
}
},
"aggs": {
"top-sites": {
"terms": {
"field": "custom.pos_format.keyword"
},
"aggs": {
"top_tags_hits": {
"top_hits": {}
}
}
}
}
}

Thanks,
Jayasree

Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

This is the icon to use if you are not using markdown format:

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.
Please update your post.

In your example, I believe that the problem is with ' . $searchstringmsg . ' which looks weird to me...

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.