简体   繁体   中英

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):

在此处输入图像描述

Now in my Lambda python code, I want to extract the "state" value. Here's my code in 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).

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']

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM