简体   繁体   English

如何在 lambda python3 中检索 JSON 字符串中的值

[英]how can i retrieve value in JSON string in lambda python3

I'm developing an AWS lambda function that is triggered from an event bridge and then putting another event using python but struggling to retrieve a value from a variable in the Json string我正在开发一个从事件桥触发的 AWS lambda 函数,然后使用 python 放置另一个事件,但努力从 Json 字符串中的变量中检索值

below is the code下面是代码

import json, boto3

client = boto3.client('events')

def lambda_handler(event, context):

testV2_dict={  
 "K1" : event['e1'] ,
"K2" : event['e2'] 
}

#converting python to json as (put_event - Details) section is expecting json
testV2=json.dumps(testV2_dict)

response = client.put_events(
         Entries=
          [
            {
             "DetailType": "test",
             "Source": "test",
             "Detail": "{ \"testK\": \"testV\",\"testK2\": \""+ testV2 +"\" }"
           }
          ]
        )

tried to add Details on different ways, "Detail": "{ \"testK\": \"testV\",\"testK2\": \""+ testV2 +"\" }" and still getting error as Malformated Details and if i deleted the ++, I'm getting word testV2 itself not the value from the above尝试以不同的方式添加详细信息, "Detail": "{ \"testK\": \"testV\",\"testK2\": \""+ testV2 +"\" }"仍然收到错误作为Malformated Details如果我删除了 ++,我得到的是testV2本身而不是上面的值

How can I retrieve the value of testV2 in the Details inside the event?如何在事件内的详细信息中检索 testV2 的值?

You don't have enough escaping in there.你没有足够的逃避那里。 If testV2 is supposed to be a JSON string emebedded in a JSON string embedded in as JSON string, then you need more string escapes.如果testV2应该是嵌入在作为 JSON 字符串嵌入的 JSON 字符串中的 JSON 字符串,那么您需要更多的字符串转义。 I would let json.dumps handle that:我会让json.dumps处理:

import json
event = {'e1': 99, 'e2': 101}

testV2_dict={  
 "K1" : event['e1'] ,
 "K2" : event['e2'] 
}

testV2=json.dumps(testV2_dict)

detail = {
    "testK": "testV",
    "testK2": testV2
}

Entries= [
    {
     "DetailType": "test",
     "Source": "test",
     "Detail": json.dumps(detail),
   }
]

print(Entries)

Output:输出:

[{'DetailType': 'test', 'Source': 'test', 'Detail': '{"testK": "testV", "testK2": "{\\"K1\\": 99, \\"K2\\": 101}"}'}]

我认为你不需要做任何事情 boto3 客户端的输出一直是 JSON 字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改字符串值我在Python3上循环 - how to change value of string i'm for-looping on Python3 python3:如何添加字符串中的小数 - python3: How can I add decimals that are inside a string 我如何在 Lambda python function 中将 dynamodb json 转换为 json - How can i convert dynamodb json to json in a Lambda python function 如何从Python3中使用Python2编码的文件中检索UTF-8编码(来自unicode)字符串的正确值? - How to retrieve correct value of a UTF-8 encoded (from unicode) string from a file from Python3 which was encoded using Python2? python3中的JSON字符串和.format() - JSON string and .format() in python3 在python中如何将字符串转换为字典以检索特定值 - In python how do I convert string to dictionary to retrieve a particular value 在python中,如何在不破坏JSON结构的情况下使用斜杠(\\)解析具有正则表达式值的JSON字符串? - In python How can I parse a JSON string, which has value regex, with slashes(\) without breaking JSON structure? 如何使用 Python 从 AWS Lambda 检索格式正确的 JSON - How to retrieve well formatted JSON from AWS Lambda using Python 我如何保存模型 python3 - How I can save the model python3 我如何在 Python3 中实现它? - How I can realize it in Python3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM