简体   繁体   English

json请求更改AWS Lambda中的json结构

[英]json request change the json structure in aws lambda

I am new to aws Lambda, I am struggling enough to find the solution dont know what to do. 我是Lambda新手,我在努力寻找解决方案不知道该怎么做。 I have lambda handler function. 我有lambda处理函数。 my code is designed in such a way it works with the last value only. 我的代码设计为仅与最后一个值一起使用。 if I pass the data inside the console like: 如果我在控制台内部传递数据,例如:

Data=[['1700','340','2040']]

and pass it to lambda function. 并将其传递给lambda函数。 it works fine. 它工作正常。 But when I use API to pass the data as API request in configure test event and print the data , it change the json structure. 但是,当我在配置测试事件中使用API​​将数据作为API请求传递并打印数据时,它将更改json结构。

json data in test event: 测试事件中的json数据:

{
    "sum_net": 1700,
    "sum_tax": 340,
    "sum_gross": 2040
}

Handler function: 处理函数:

def handler(event,extract)

data= event.values()

return 

result change the json object order. 结果更改json对象的顺序。 values are not the same as the manually defined dictionary. 值与手动定义的字典不同。 is there any way to keep the structure same.problem appears when json object changes to python dict. 有什么办法可以保持结构相同。当json对象更改为python dict时出现问题。 it changes the order of keys and values 它改变了键和值的顺序
Thank you in advance. 先感谢您。

Extracted the answer from JSON order mixed up : 混合的JSON顺序中提取答案:

You may not rely on the ordering of elements within a JSON object. 您可能不依赖JSON对象中元素的顺序。

From the JSON specification at http://www.json.org/ 来自http://www.json.org/的JSON规范

An object is an unordered set of name/value pairs 对象是名称/值对的无序集合

As a consequence, lambda and any other JSON libraries are free to rearrange the order of the elements as they see fit. 因此,lambda和任何其他JSON库都可以自由地按自己认为合适的方式重新排列元素的顺序。

The only way to change what lambda returns is to change lambda. 更改lambda返回值的唯一方法是更改​​lambda。 If it is not an open-source AWS library and not open-source, it may not be easy at all. 如果它不是开源的AWS库也不是开源的,则可能根本不容易。

If you know which is the value that you want from the handler, "sum_gross" I assume, you can directly get this value with the dictionary.get("key") method: 如果您知道要从处理程序中获取哪个值,即"sum_gross" ,则可以使用dictionary.get("key")方法直接获取此值:

def handler(event,extract)

    data = event.get("sum_gross");

return 

more info about python dictionaries: http://www.tutorialspoint.com/python/dictionary_get.htm 有关python词典的更多信息: http : //www.tutorialspoint.com/python/dictionary_get.htm

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

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