I have a json object returned from an elasticsearch query as follows:
{"took": 10, "timed_out": false, "_shards": {"total": 5, "successful": 5, "skipped": 0, "failed": 0}, "hits": {"total": 1, "max_score": 0.5753642, "hits": [{"_index": "match", "_type": "score", "_id": "J_J1zGcBjpp4O0gQqUq_", "_score": 0.5753642, "_source": {"tournament_id": 1, "board_number": "2", "nsp": "1", "ewp": "8", "contract": "3C", "by": "N", "tricks": "9", "nsscore": "110", "ewscore": "0", "timestamp": "2018-12-20T16:32:02.440315"}}]}}
I need to access the "nsscore" element. When I try the following:
print(["hits"]["hits"]["_source"]["nsscore"])
I get the following error:
print(["hits"]["hits"]["_source"]["nsscore"])
TypeError: list indices must be integers or slices, not str
Wht is the correct way to access this element? Thank you!
You're missing a variable name here. For example, if:
variable = {"took": 10, "timed_out": false, "_shards": {"total": 5, "successful": 5, "skipped": 0, "failed": 0}, "hits": {"total": 1, "max_score": 0.5753642, "hits": [{"_index": "match", "_type": "score", "_id": "J_J1zGcBjpp4O0gQqUq_", "_score": 0.5753642, "_source": {"tournament_id": 1, "board_number": "2", "nsp": "1", "ewp": "8", "contract": "3C", "by": "N", "tricks": "9", "nsscore": "110", "ewscore": "0", "timestamp": "2018-12-20T16:32:02.440315"}}]}}
Then:
print(variable["hits"]["hits"][0]["_source"]["nsscore"])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.