# JSON Parse error

**URL:** <https://discuss.elastic.co/t/json-parse-error/59251>\
**Category:** Elasticsearch\
**Created:** [August 29, 2016, 11:34pm UTC](https://discuss.elastic.co/t/json-parse-error/59251 "2016-08-29T23:34:56Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pstarconsult](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pstarconsult](https://discuss.elastic.co/u/pstarconsult)\
**Post date:** [August 29, 2016, 11:34pm UTC](https://discuss.elastic.co/t/json-parse-error/59251/1 "2016-08-29T23:34:56Z")

</div>

I get a parse error when trying to index a JSON file. I am running this from the Python API for ES and, as I step deep into the code, it appears to be based on the format that Tika generates. I have a very simple TXT file (with a few sentences in it) which I ran through Tika converted to JSON. When I check at jsonlint it shows as valid JSON. After stepping into the code, it appears to be an issue with the fact that Tika uses square brackets at the beginning and end of the file rather than curly braces. So, it is valid JSON but chokes when I try to index in ES. I would attach the file but it is not an allowed file type.

Has anyone had this issue with Tika? It is such a ubiquitous tool that I find it odd that the file will not index easily. Is there another tool that I can use that will convert many file types (as many as Tika) that does work? Just so it is here, I am using sample code provided by Elastic PY here (endpoint URL omitted); error occurs at res.index:

es = Elasticsearch('https://[omitted].com:30737')  
res = requests.get('https:/[omitted]com:30737')  
print(res.content)

f = open("content.txt.json", "r")  
content = json.load(f)  
f.close()  
res = es.index(index="json-test-index", doc\_type='test', id=2, body=content)  
print(res['created'])

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [August 29, 2016, 11:54pm UTC](https://discuss.elastic.co/t/json-parse-error/59251/2 "2016-08-29T23:54:01Z")

</div>

Elasticsearch needs its documents to be a single json object but it sounds  
like Tika is spitting out a list. You have to concert it.

---

<div class="post-metadata">

**Author:** ![pstarconsult](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pstarconsult](https://discuss.elastic.co/u/pstarconsult)\
**Post date:** [August 30, 2016, 3:25am UTC](https://discuss.elastic.co/t/json-parse-error/59251/3 "2016-08-30T03:25:55Z")

</div>

Thank you for the idea. First, I thought it was the json.load call that was the culprit since I think that converts the JSON to a list. I performed just a file.read() instead and get the same error. I then imported simplejson and tried json.load with that, same error. Not sure what to do but this is a total show stopper for me. Greatly appreciate any other ideas! (I am pasting the file content below):

[{"Content-Encoding":"windows-1252","Content-Length":"508","Content-Type":"text/plain; charset\u003dwindows-1252","X-Parsed-By":["org.apache.tika.parser.DefaultParser","org.apache.tika.parser.txt.TXTParser"],"X-TIKA:content":"\u003chtml xmlns\u003d"[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)"\u003e\n\u003chead\u003e\n\u003cmeta name\u003d"X-Parsed-By" content\u003d"org.apache.tika.parser.DefaultParser" /\u003e\n\u003cmeta name\u003d"X-Parsed-By" content\u003d"org.apache.tika.parser.txt.TXTParser" /\u003e\n\u003cmeta name\u003d"tika:file\_ext" content\u003d"txt" /\u003e\n\u003cmeta name\u003d"Content-Encoding" content\u003d"windows-1252" /\u003e\n\u003cmeta name\u003d"tika\_batch\_fs:relative\_path" content\u003d"content1.txt" /\u003e\n\u003cmeta name\u003d"resourceName" content\u003d"content1.txt" /\u003e\n\u003cmeta name\u003d"Content-Length" content\u003d"508" /\u003e\n\u003cmeta name\u003d"X-TIKA:digest:MD5" content\u003d"8ac30237b478064c3f595e6f71189728" /\u003e\n\u003cmeta name\u003d"Content-Type" content\u003d"text/plain; charset\u003dwindows-1252" /\u003e\n\u003ctitle\u003e\u003c/title\u003e\n\u003c/head\u003e\n\u003cbody\u003e\u003cp\u003eTry the Elastic Stack 5.0.0-alpha5, and join the Elastic Pioneer Program\r\n\r\nThe 5.0.0-alpha5 release candidates of Elasticsearch, Logstash, Beats, Kibana, and X-Pack are now available to download and test! Learn about the releases here.\r\n\r\nAs you try out the alpha5, be sure to open any issues in the GitHub repos for the projects (for X-Pack, post in the X-Pack category on Discuss). As a thank you for reporting issues, you will receive special gifts and recognition as part of the Elastic Pioneer Program.\u003c/p\u003e\n\u003c/body\u003e\u003c/html\u003e","X-TIKA:digest:MD5":"8ac30237b478064c3f595e6f71189728","X-TIKA:parse\_time\_millis":"109","resourceName":"content1.txt","tika:file\_ext":"txt","tika\_batch\_fs:relative\_path":"content1.txt"}]

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [August 30, 2016, 6:01am UTC](https://discuss.elastic.co/t/json-parse-error/59251/4 "2016-08-30T06:01:03Z")

</div>

Well, the outermost entity _is_ a list. No JSON library in the world is going to say otherwise. Instead of passing the whole parsed entity to ES just pass the first element of the list.

---

<div class="post-metadata">

**Author:** ![pstarconsult](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pstarconsult](https://discuss.elastic.co/u/pstarconsult)\
**Post date:** [August 30, 2016, 6:32am UTC](https://discuss.elastic.co/t/json-parse-error/59251/5 "2016-08-30T06:32:57Z")

</div>

Do you mean pass in everything inside the square brackets? I tried that and I get the same error. However I think you may mean something different.

Do you know of another file conversion tool other than Tika that ES users use often? That might be easier.

Thank you very much!

---

<div class="post-metadata">

**Author:** ![pstarconsult](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pstarconsult](https://discuss.elastic.co/u/pstarconsult)\
**Post date:** [August 30, 2016, 3:14pm UTC](https://discuss.elastic.co/t/json-parse-error/59251/6 "2016-08-30T15:14:44Z")

</div>

One more thing. Even though this is a list, it still tests as valid JSON at jsonlnt. Thx.

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [August 30, 2016, 5:36pm UTC](https://discuss.elastic.co/t/json-parse-error/59251/7 "2016-08-30T17:36:18Z")

</div>

> Do you mean pass in everything inside the square brackets?

Yes.

> I tried that and I get the same error.

Please show the actual error message.

---

<div class="post-metadata">

**Author:** ![pstarconsult](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pstarconsult](https://discuss.elastic.co/u/pstarconsult)\
**Post date:** [August 30, 2016, 5:57pm UTC](https://discuss.elastic.co/t/json-parse-error/59251/8 "2016-08-30T17:57:16Z")

</div>

The stack trace is below. I am also attaching two screenshots of where in the code the error occurs (not sure if this provides useful info). Thank you!

PUT /json-test-index/test/2 [status:400 request:0.392s]  
Traceback (most recent call last):  
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.1.4\helpers\pydev\pydevd.py", line 1531, in   
globals = debugger.run(setup['file'], None, None, is\_module)  
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.1.4\helpers\pydev\pydevd.py", line 938, in run  
pydev\_imports.execfile(file, globals, locals) # execute the script  
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.1.4\helpers\pydev\_pydev\_imps\_pydev\_execfile.py", line 18, in execfile  
exec(compile(contents+"\n", file, 'exec'), glob, loc)  
File "C:/Text\_Pipeline/Elastic/elastic.py", line 35, in   
res = es.index(index="json-test-index", doc\_type='test', id=2, body=content)  
File "C:\Users\Paul Starret\AppData\Roaming\Python\Python34\site-packages\elasticsearch\client\utils.py", line 69, in _wrapped  
return func(\*args, params=params, \*\*kwargs)  
File "C:\Users\Paul Starret\AppData\Roaming\Python\Python34\site-packages\elasticsearch\client\_init_.py", line 279, in index  
\_make\_path(index, doc\_type, id), params=params, body=body)  
File "C:\Users\Paul Starret\AppData\Roaming\Python\Python34\site-packages\elasticsearch\transport.py", line 327, in perform\_request  
status, headers, data = connection.perform\_request(method, url, params, body, ignore=ignore, timeout=timeout)  
File "C:\Users\Paul Starret\AppData\Roaming\Python\Python34\site-packages\elasticsearch\connection\http\_urllib3.py", line 109, in perform\_request  
self.\_raise\_error(response.status, raw\_data)  
File "C:\Users\Paul Starret\AppData\Roaming\Python\Python34\site-packages\elasticsearch\connection\base.py", line 113, in \_raise\_error  
raise HTTP\_EXCEPTIONS.get(status\_code, TransportError)(status\_code, error\_message, additional\_info)  
elasticsearch.exceptions.RequestError: TransportError(400, 'mapper\_parsing\_exception', 'failed to parse')

 ![](https://us1.discourse-cdn.com/elastic/original/2X/6/6613016f1cd9c294a7e512451aac02461d8a9beb.png)

![](https://us1.discourse-cdn.com/elastic/original/2X/4/44b99313c57c1bc3e2499c976513e42a1fa6a426.png)

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [August 30, 2016, 6:00pm UTC](https://discuss.elastic.co/t/json-parse-error/59251/9 "2016-08-30T18:00:56Z")

</div>

Weird version of elasticsearch-py are you using?

---

<div class="post-metadata">

**Author:** ![pstarconsult](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pstarconsult](https://discuss.elastic.co/u/pstarconsult)\
**Post date:** [August 30, 2016, 6:30pm UTC](https://discuss.elastic.co/t/json-parse-error/59251/10 "2016-08-30T18:30:26Z")

</div>

I am using Elasticsearch PY version 2.4 with Python 3.4.

Paul Starrett, CEO, Starrett Consulting, Inc.

[www.starrettconsultinginc.com](http://www.starrettconsultinginc.com)[http://www.starrettconsultinginc.com](http://www.starrettconsultinginc.com)  
Licensed Private Investigator and Attorney  
Certified Fraud Examiner  
EnCase Certified Computer Forensics Examiner (EnCE)  
Master of Science in Predictive Analytics (Northwestern U.)

---

<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 5, 2017, 10:23pm UTC](https://discuss.elastic.co/t/json-parse-error/59251/11 "2017-07-05T22:23:55Z")

</div>


