# How to get a json object in script queries

**URL:** <https://discuss.elastic.co/t/how-to-get-a-json-object-in-script-queries/143959>\
**Category:** Elasticsearch\
**Created:** [August 11, 2018, 7:07am UTC](https://discuss.elastic.co/t/how-to-get-a-json-object-in-script-queries/143959 "2018-08-11T07:07:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackiecookie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jackiecookie/32/34398_2.png) [@jackiecookie](https://discuss.elastic.co/u/jackiecookie)\
**Post date:** [August 11, 2018, 7:07am UTC](https://discuss.elastic.co/t/how-to-get-a-json-object-in-script-queries/143959/1 "2018-08-11T07:07:49Z")

</div>

my elasticsearch version is 6.3.0,and here is my query:

```
{
  "query":{
  "bool" : {
    "must" : [
      {
        "script" : {
          "script" : {
            "source" : "double minPrice=-1;int userLevel=params.defaultLevel;long userId=params.userId;if(params._source.containsKey('userLevels')){ for(int i = 0; i < params['_source']['userLevels'].length; ++i) { def userLevelObj = params['_source']['userLevels'][i]; if(userLevelObj.userId === userId) { userLevel = userLevelObj.level; } }}for(int ii = 0; ii < params['_source']['productSkus'].length; ++ii){ def productSku = params['_source']['productSkus'][ii]; for(int iii = 0; iii < productSku['levelPrice'].length; ++iii) { def levelPrice = productSku['levelPrice'][iii]; if(userLevel === levelPrice.level && (levelPrice.price < minPrice || minPrice === -1)) { minPrice=levelPrice.price; } }}return minPrice<=params.maxPrice;",
            "lang" : "painless",
            "params" : {
              "defaultLevel" : 1,
              "maxPrice" : 10000,
              "userId" : 123
            }
          },
          "boost" : 1.0
        }
      },
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}
}

```

and i am getting following error:

```
{
  "took": 17,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 4,
    "skipped": 0,
    "failed": 1,
    "failures": [
      {
        "shard": 3,
        "index": "product_1",
        "node": "wrIbN9oaTXGgRjDJecx8jA",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "if(params._source.containsKey('userLevels')){ ",
            " ^---- HERE"
          ],
          "script": "double minPrice=-1;int userLevel=params.defaultLevel;long userId=params.userId;if(params._source.containsKey('userLevels')){ for(int i = 0; i < params['_source']['userLevels'].length; ++i) { def userLevelObj = params['_source']['userLevels'][i]; if(userLevelObj.userId === userId) { userLevel = userLevelObj.level; } }}for(int ii = 0; ii < params['_source']['productSkus'].length; ++ii){ def productSku = params['_source']['productSkus'][ii]; for(int iii = 0; iii < productSku['levelPrice'].length; ++iii) { def levelPrice = productSku['levelPrice'][iii]; if(userLevel === levelPrice.level && (levelPrice.price < minPrice || minPrice === -1)) { minPrice=levelPrice.price; } }}return minPrice<=params.maxPrice;",
          "lang": "painless",
          "caused_by": {
            "type": "null_pointer_exception",
            "reason": null
          }
        }
      }
    ]
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

```

and according to the document, I know the `_source` is not accessible in script queries,and `doc[...]` notation only allows for simple valued fields. but how to get a json object in script queries. or anyone can give me any suggestion to optimize my query scripy. thinks

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [August 12, 2018, 6:45am UTC](https://discuss.elastic.co/t/how-to-get-a-json-object-in-script-queries/143959/2 "2018-08-12T06:45:19Z")

</div>

This error occurred because, there's no key `_source` in your params object:

```
"params" : {
      "defaultLevel" : 1,
      "maxPrice" : 10000,
      "userId" : 123
}

```

I think you can directly call `params.containsKey('userLevels')`

---

<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:** [September 9, 2018, 6:45am UTC](https://discuss.elastic.co/t/how-to-get-a-json-object-in-script-queries/143959/3 "2018-09-09T06:45:36Z")

</div>

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