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