Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在aws lambda中处理和提取事件json数据?_Python_Json_Amazon Web Services_Aws Lambda_Aws Api Gateway - Fatal编程技术网

Python 如何在aws lambda中处理和提取事件json数据?

Python 如何在aws lambda中处理和提取事件json数据?,python,json,amazon-web-services,aws-lambda,aws-api-gateway,Python,Json,Amazon Web Services,Aws Lambda,Aws Api Gateway,如果我有JSON,比如这是我的事件体,如果我想获得object_属性中的标题,并且我正在通过Postman通过body中的Post请求将此数据发送到我的API,那么有人能帮我获得类似的值吗 "object_kind": "issue", "event_type": "issue", "user": { "id": 1, "name"

如果我有JSON,比如这是我的事件体,如果我想获得object_属性中的标题,并且我正在通过Postman通过body中的Post请求将此数据发送到我的API,那么有人能帮我获得类似的值吗

  "object_kind": "issue",
  "event_type": "issue",
  "user": {
    "id": 1,
    "name": "Administrator",
    "username": "root",
    "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon",
    "email": "admin@example.com"
  },
  "project": {
    "id": 1,
    "name":"Gitlab Test",
    "description":"Aut reprehenderit ut est.",
    "web_url":"http://example.com/gitlabhq/gitlab-test",
    "avatar_url":null,
    "git_ssh_url":"git@example.com:gitlabhq/gitlab-test.git",
    "git_http_url":"http://example.com/gitlabhq/gitlab-test.git",
    "namespace":"GitlabHQ",
    "visibility_level":20,
    "path_with_namespace":"gitlabhq/gitlab-test",
    "default_branch":"master",
    "ci_config_path": null,
    "homepage":"http://example.com/gitlabhq/gitlab-test",
    "url":"http://example.com/gitlabhq/gitlab-test.git",
    "ssh_url":"git@example.com:gitlabhq/gitlab-test.git",
    "http_url":"http://example.com/gitlabhq/gitlab-test.git"
  },
  "object_attributes": {
    "id": 301,
    "title": "New API: create/update/delete file",
    "assignee_ids": [51],
    "assignee_id": 51,
    "author_id": 51,
    "project_id": 14,
    "created_at": "2013-12-03T17:15:43Z",
    "updated_at": "2013-12-03T17:15:43Z",
    "updated_by_id": 1,
    "last_edited_at": null,
    "last_edited_by_id": null,
    "relative_position": 0,
    "description": "Create new API for manipulations with repository",
    "milestone_id": null,
    "state_id": 1,
    "confidential": false,
    "discussion_locked": true,
    "due_date": null,
    "moved_to_id": null,
    "duplicated_to_id": null,
    "time_estimate": 0,
    "total_time_spent": 0,
    "human_total_time_spent": null,
    "human_time_estimate": null,
    "weight": null,
    "iid": 23,
    "url": "http://example.com/diaspora/issues/23",
    "state": "opened",
    "action": "open",
    "labels": [{
        "id": 206,
        "title": "API",
        "color": "#ffffff",
        "project_id": 14,
        "created_at": "2013-12-03T17:15:43Z",
        "updated_at": "2013-12-03T17:15:43Z",
        "template": false,
        "description": "API related issues",
        "type": "ProjectLabel",
        "group_id": 41
      }]
  },
  "repository": {
    "name": "Gitlab Test",
    "url": "http://example.com/gitlabhq/gitlab-test.git",
    "description": "Aut reprehenderit ut est.",
    "homepage": "http://example.com/gitlabhq/gitlab-test"
  },
  "assignees": [{
    "name": "User1",
    "username": "user1",
    "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
  }],
  "assignee": {
    "name": "User1",
    "username": "user1",
    "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
  },
  "labels": [{
    "id": 206,
    "title": "API",
    "color": "#ffffff",
    "project_id": 14,
    "created_at": "2013-12-03T17:15:43Z",
    "updated_at": "2013-12-03T17:15:43Z",
    "template": false,
    "description": "API related issues",
    "type": "ProjectLabel",
    "group_id": 41
  }],
  "changes": {
    "updated_by_id": {
      "previous": null,
      "current": 1
    },
    "updated_at": {
      "previous": "2017-09-15 16:50:55 UTC",
      "current": "2017-09-15 16:52:00 UTC"
    },
    "labels": {
      "previous": [{
        "id": 206,
        "title": "API",
        "color": "#ffffff",
        "project_id": 14,
        "created_at": "2013-12-03T17:15:43Z",
        "updated_at": "2013-12-03T17:15:43Z",
        "template": false,
        "description": "API related issues",
        "type": "ProjectLabel",
        "group_id": 41
      }],
      "current": [{
        "id": 205,
        "title": "Platform",
        "color": "#123123",
        "project_id": 14,
        "created_at": "2013-12-03T17:15:43Z",
        "updated_at": "2013-12-03T17:15:43Z",
        "template": false,
        "description": "Platform related issues",
        "type": "ProjectLabel",
        "group_id": 41
      }]
    }
  }
}
 

  
   

在案例1中,我已经在lambda_处理器中尝试过这一点:

import json

def lambda_handler(event, context):
    parsed=json.loads(event['body'])
    response={
     "statuscode":200,
     "body":parsed
      }
    return response

我得到了完整的json响应和响应代码200,但当我尝试这种情况2时:

import json

def lambda_handler(event, context):
    parsed=json.loads(event['body']['closed_by'])
    response={
     "statuscode":200,
     "body":parsed
      }
    return response
我的邮递员中有500个内部服务器错误,因为有人可以帮助我在JSON中达到这一点

cloudwatch内部的错误是:

[ERROR] TypeError:string indices must be integer.
Traceback:(Most recent call last):
File"/var/task/lambda_function.py",line 6,in lambda_handler
  parsed=json.loads(event['body']['object_attributes'])

注:这是我关于stackoverflow的第一个问题,请提前感谢。

您遇到了什么错误?请始终发布错误。对不起,我在这里发布了关于打印的内容,但实际上,我正在发送一个响应,并在案例1的邮递员上测试它。我收到200个响应,而在第二个案例500内部服务器错误。这是响应代码。CloudWatch日志中显示的错误消息是什么?似乎您的事件['body']['closed_by']不存在如果您更靠近Json内容如果您想访问'closed_by'元素,您只需编写事件['closed_by']。实际上,这个元素位于json事件的根。我很想知道打印的是什么事件['body')?正如@jens所提到的,这里的错误将非常有用。我还想知道为什么你要
json.dumps
这个
event
参数,而你似乎已经可以将它作为一个字典来访问了,所以只需打印字典就可以了