Execute json file of "query and painless script" from the python script

Hi all,

I'd like to execute json file of "query and painless script" from the python script made by other.
However, when python script is executed, "simplejson.errors.JSONDecodeError" is output.
The error points the last of double quotation ["source": "] in painless script.

If I use JSONSerializer within Python Elasticsearch Client, can json file of "query and painless script" be executed from python script?
Since I have no python experience, I need to confirm it in advance.
https://elasticsearch-py.readthedocs.io/en/master/index.html#custom-serializers


  • JUST json file example for query and painless script.
    GET school/_search
    {
      "query": {
        "match_all": {}
      },
      "script_fields": {
        "test": {
          "script": {
            "lang": "painless",
            "source": " <======== ERROR!!!
              return params._source.school_name
            "
          }
        }
      }
    }

  • simplejson.errors.JSONDecodeError
    Traceback (most recent call last):
      File "run_test_elsticsearch.py", line 406, in <module>
        me = JobStart(processes, job_id)
      File "run_test_elsticsearch.py", line 114, in __init__
        self.conf = self.read_conf(conf_filename)
      File "run_test_elsticsearch.py", line 153, in read_conf
        return json.load(fp)
      File "/usr/local/lib/python2.7/dist-packages/simplejson/__init__.py", line 461, in load
        use_decimal=use_decimal, **kw)
      File "/usr/local/lib/python2.7/dist-packages/simplejson/__init__.py", line 518, in loads
        return _default_decoder.decode(s)
      File "/usr/local/lib/python2.7/dist-packages/simplejson/decoder.py", line 370, in decode
        obj, end = self.raw_decode(s)
      File "/usr/local/lib/python2.7/dist-packages/simplejson/decoder.py", line 400, in raw_decode
        return self.scan_once(s, idx=_w(s, idx).end())
    simplejson.errors.JSONDecodeError: Invalid control character '\n' at: line 39 column 26 (char 1072)

The JSON spec does not allow for newline characters within strings. You need to remove the newlines at the beginning and end of your script.

1 Like

Hi Ryan,

Thank you for your comment.
I tested small painless script without "\n" yesterday.
Seem like it works.
After I verify it more, I will post the result.

Hi Ryan and all,

I wrote painless script in one line without line breaks.
Finally it worked!

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