Elasticsearch \ RuntimeException:JSON编码失败:第33行文件/Elasticsearch/Serializers/SmartSerializer.php中的5

I am trying to run a project that uses elasticsearch on my computer. But at the point that the API. Queries something from elasticsearch I get the following error and the query does not execute:

Elasticsearch\Common\Exceptions\RuntimeException: Failed to JSON encode: 5 in file /Users/sobhan.bagheri/projects/laravel/regionsdelen-api/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Serializers/SmartSerializer.php on line 33

I am using: laravel framework with valet - php7.1 - elasticsearch 6.1.2 - and elasticsearch/elasticsearch:v6.0.1 for the driver.

I tried everything like changing elasticsearch to newer or older versions. Changing the way of the client connects to elasticsearch and also changed versions of the elasticsearch/elasticsearch driver but nothing seems to work.

btw: those versions I mentioned are the exact ones running on another machine without this error.

P.S: This error only occurres when I query a swedish character.

I also was facing the same problem. For me, the issue was that string contained special character which creates an issue while encoding to JSON so try this.

$array= array_map('utf8_encode', $array);

Hopefully, this will work for you.