简体   繁体   English

使用带有 EMR 的 AWS Lambda 从字符串中获取 JSON object

[英]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.

相关问题 使用 AWS Lambda 启动 Emr - Emr Launch with AWS Lambda Compress large string into json serializable object for AWS lambda function output - Compress large string into json serializable object for AWS lambda function output 使用带有 python 或 java 代码的 AWS lambda 从 RestAPI 调用中获取 JSON 并插入到 oracle 数据库中 - Using AWS lambda with python or java code to get JSON from RestAPI call and insert into oracle DB 如何获取在 s3 中上传的最新 object 名称并使用 AWS Lambda 将该数据(json)推送到不同的 api(点击 api)? - How do I get the latest object name uploaded in s3 and push that data(json) to a different api (hit an api) using AWS Lambda? 如何使用 Python 从 AWS Lambda 检索格式正确的 JSON - How to retrieve well formatted JSON from AWS Lambda using Python AWS Lambda 以字符串形式返回 JSON 数据 - AWS Lambda returning JSON data as string 无法在 AWS EMR 中使用 Pyspark 或 Python 从 mongoDB 读取数据 - Unable to read data from mongoDB using Pyspark or Python in AWS EMR 使用AWS Lambda从AWS SNS读取时修改JSON消息 - Modify JSON message while reading from AWS SNS using AWS Lambda 如何将 Terraform object 放入 AWS Lambda 环境 - How to get a Terraform object into an AWS Lambda environment 格式化来自 AWS Lambda 函数的 JSON 输出 - Format JSON output from AWS Lambda function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM