[英]get a JSON object from a string using AWS Lambda with EMR
So, in AWS EMR CLI, running the following commnand outputs a JSON like this (values are invalid now, so no fear of leaking sensitive info):因此,在 AWS EMR CLI 中,运行以下命令会输出这样的 JSON(值现在无效,所以不用担心泄露敏感信息):
Now in my Lambda python code, I want to extract the "state" value.现在在我的 Lambda python 代码中,我想提取“状态”值。 Here's my code in python:
这是我在 python 中的代码:
import json
import requests
def lambda_handler(event, context):
jobid = event.get('jobID')
url = '<public DNS of my EMR>/batches/' + str(jobid)
res = requests.get(url)
json_data = json.loads(res.text)
return json_data.get('state')
I get an error like this:我收到这样的错误:
{
"errorMessage": "'str' object has no attribute 'get'",
"errorType": "AttributeError",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 9, in lambda_handler\n return json_data.get('state')\n"
]
}
What am I doing wrong here and how do I correct it?我在这里做错了什么,我该如何纠正?
Since you are using the requests module, you can use the json()
method as explained here to return the data as a JSON-like structure (dict).由于您使用的是requests模块,因此您可以使用此处解释的
json()
方法以类似 JSON 的结构 (dict) 形式返回数据。
So, you can do something like this:因此,您可以执行以下操作:
import requests
import json
def lambda_handler(event, context):
jobid = event.get('jobID')
url = '<public DNS of my EMR>/batches/' + str(jobid)
res = requests.get(url)
js_res = res.json()
return js_res['state']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.