# Invalid UTF-8 middle byte 0x22\\n

**URL:** <https://discuss.elastic.co/t/invalid-utf-8-middle-byte-0x22-n/236047>\
**Category:** Elasticsearch\
**Created:** [June 6, 2020, 5:10pm UTC](https://discuss.elastic.co/t/invalid-utf-8-middle-byte-0x22-n/236047 "2020-06-06T17:10:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joaopedro](https://avatars.discourse-cdn.com/v4/letter/j/7ab992/32.png) [@joaopedro](https://discuss.elastic.co/u/joaopedro)\
**Post date:** [June 6, 2020, 5:10pm UTC](https://discuss.elastic.co/t/invalid-utf-8-middle-byte-0x22-n/236047/1 "2020-06-06T17:10:38Z")

</div>

So, I am trying to create a flask api that gets the name of the field of search and the term that we are going to search, then, the script writes the info into a file and uses subprocess to run a search, see:

```auto
file = open("filtro_match.json")
                filtro = file.read()
                file.close()
                lista = word.split("|")
                filtro = filtro.replace("$campo",str(lista[0]))
                filtro = filtro.replace("$busca",str(lista[1]))
                #return "Ok"
                file = open("filtro_match_temp.json", "w")
                file.write(filtro)
                file.close()
                resultado = subprocess.check_output('curl -XGET "localhost:9200/pessoas/_search?pretty" -H "Content-Type: application/json" --data @filtro_match_temp.json')
                temp = resultado.decode('utf-8')
                obj = json.loads(temp)

```

However, everytime that I try to run it,I receive this error:

```auto
"type" : "json_parse_exception",
        "reason" : "Invalid UTF-8 middle byte 0x22\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@f868020; line: 1, column: 39]"

```

I have no idea from where this "Invalid UTF-8 middle byte" came from, I write the file with python, can someone help me out?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 7, 2020, 1:52am UTC](https://discuss.elastic.co/t/invalid-utf-8-middle-byte-0x22-n/236047/2 "2020-06-07T01:52:50Z")

</div>

A guess is that your json file is encoded with another format than UTF8

---

<div class="post-metadata">

**Author:** ![joaopedro](https://avatars.discourse-cdn.com/v4/letter/j/7ab992/32.png) [@joaopedro](https://discuss.elastic.co/u/joaopedro)\
**Post date:** [June 7, 2020, 11:59am UTC](https://discuss.elastic.co/t/invalid-utf-8-middle-byte-0x22-n/236047/3 "2020-06-07T11:59:05Z")

</div>

Yeah, I realized a little later....

But what I can do? I am brazilian and we have a lot of non utf-8 in our names (and my project deal both with people and place names). Is there someway to make elastic or json accept the non utf-8 characters to make a search? It do allows it to be stored but not to make a search.

If there is no way, could you help me with updating the database? I mean, how I can update one field (let's say "name") of a entrance in the database without updating all the rest?

PS: thanks for the answer!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 26, 2020, 8:44am UTC](https://discuss.elastic.co/t/invalid-utf-8-middle-byte-0x22-n/236047/4 "2020-06-26T08:44:55Z")

</div>

> [@joaopedro](#):
>
> Is there someway to make elastic or json accept the non utf-8 characters to make a search?

I don't think so.

> [@joaopedro](#):
>
> Is there someway to make elastic or json accept the non utf-8 characters to make a search?

I don't know how to do this is Python but I guess you could do something like this (Java code):

```auto
String json = "YOUR CONTENT ENCODED WITH YOUR ENCODING";
String jsonUtf8 = new String(json.getBytes("YOUR_ENCODING"), "UTF-8");

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 24, 2020, 8:44am UTC](https://discuss.elastic.co/t/invalid-utf-8-middle-byte-0x22-n/236047/5 "2020-07-24T08:44:58Z")

</div>

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