Signup/Sign In
LAST UPDATED: AUGUST 23, 2021

[SOLVED] Unexpected character: Parse Exception at Source: ByteBufStreamInput class in Elasticsearch API

    If you are trying to run a curl request for elasticsearch API to add or update any settings in elasticsearch or may be add a new index or add a JSON document, and you have got the following error:

    {
      "error" : {
        "root_cause" : [
          {
            "type" : "parse_exception",
            "reason" : "Failed to parse content to map"
          }
        ],
        "type" : "parse_exception",
        "reason" : "Failed to parse content to map",
        "caused_by" : {
          "type" : "json_parse_exception",
          "reason" : "Unexpected character ('}' (code 125)): was expecting double-quote to start field name\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@e32aa8a; line: 1, column: 102]"
        }
      },
      "status" : 400
    }
    

    or something similar to it, then you are at the right place to resolve it.

    If you are an Elasticsearch beginner, then you must learn Elasticsearch first. We have a simple and well explained tutorial covering all the major features of Elasticsearch.

    Is the JSON valid?

    There can be multiple reasons for this error, the first one being, you may have incorrect JSON which you are using as body content, so the first thing that you must do is, head straigth to jsonlint.com and verify whether your JSON string is correct.

    Chances are that verifying your JSON will solve your issue, because most of the time, the issue is with the JSON where we forget to remove the comma from the last field in the JSON string, or may be a mismatched double quote, etc.

    If there is no issue with your JSON, then we are dealing with an interesting issue here.

    Are you using Windows Command Prompt?

    If you are a Windows user and you are currently running this curl request in your Windows machine, then, the issue can be with the use of single quote in the curl request.

    The standard curl request that we get from the official elasticsearch website is something like this:

    curl -X PUT "localhost:9200/_template/template_1?pretty" -H 'Content-Type: application/json' -d'
    {
      "index_patterns": ["te*", "bar*"],
      "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 2
      }
    }
    '
    

    This is a PUT request for creating a new index template, to provide settings to be used whenever a new index is created in elasticsearch.

    If you look closely, in this curl request, we have used single quotes to enclose the JSON message, then the value of the Header for the curl request is also enclosed within single quotes, but in case of Windows command prompt, single quote is not supported, so we must use double quotes instead and we can use \ to escape the inner double quotes.

    The above curl request will look like this:

    curl -X PUT "localhost:9200/_template/template_1?pretty" -H "Content-Type: application/json" -d"
    {
      \"index_patterns\": [\"te*\", \"bar*\"],
      \"settings\": {
        \"number_of_shards\": 3,
        \"number_of_replicas\": 2
      }
    }
    "

    Similarly, if you are running a curl request to execute a Query in Elasticsearch to get some data, or may be create an index, you can use the same approach if you are working on a windows machine.

    Conclusion:

    Hope this article helps you in resolving the issue. If it did, you can post a small comment in the comment section below, so that we know that we are helping developers around the world. If you are still facing the issue, then share the error and and the curl request in the comments, so that we can help you resolve the issue.

    You may also like:

    I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS