Skip to content

Instantly share code, notes, and snippets.

@suganoo
Last active July 23, 2018 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suganoo/3b2300e95d58a49007cc07b8ac53a938 to your computer and use it in GitHub Desktop.
Save suganoo/3b2300e95d58a49007cc07b8ac53a938 to your computer and use it in GitHub Desktop.
json パースでTypeError: string indices must be integers
self.filepath = self._get_value_from_messageattributes("filepath")
File "/xxxxxx/test.py", line 93, in _get_value_from_messageattributes
self.logger.debug("return value : %s", self.message["Message"][key]["Value"])
TypeError: string indices must be integers
{
"Subject" : "testtesttest",
"Message" : {
"filepath" : {"Value":"/xxxxxx/hogehogehoge.txt"}
}
}
import json
def _get_value_from_messageattributes(self, key):
# 一回読み込んで....
self.message = json.loads(message)
"""
"Message"以下が単なるStringになってたので下記はエラー
self.logger.debug("return value : %s", self.message["Message"][key]["Value"])
"""
# "Message"配下のStringを再度json形式で読み込む
msg_json = json.loads(self.message["Message"])
self.logger.debug("return value : %s", msg_json[key]["Value"])
return msg_json[key]["Value"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment