<i id='HWR5J'><tr id='HWR5J'><dt id='HWR5J'><q id='HWR5J'><span id='HWR5J'><b id='HWR5J'><form id='HWR5J'><ins id='HWR5J'></ins><ul id='HWR5J'></ul><sub id='HWR5J'></sub></form><legend id='HWR5J'></legend><bdo id='HWR5J'><pre id='HWR5J'><center id='HWR5J'></center></pre></bdo></b><th id='HWR5J'></th></span></q></dt></tr></i><div id='HWR5J'><tfoot id='HWR5J'></tfoot><dl id='HWR5J'><fieldset id='HWR5J'></fieldset></dl></div>

      <small id='HWR5J'></small><noframes id='HWR5J'>

        <bdo id='HWR5J'></bdo><ul id='HWR5J'></ul>

      <legend id='HWR5J'><style id='HWR5J'><dir id='HWR5J'><q id='HWR5J'></q></dir></style></legend>

      <tfoot id='HWR5J'></tfoot>
    1. Python Lambda 函数解析 DynamoDB 的 JSON 格式

      Python Lambda Function Parsing DynamoDB#39;s JSON Format(Python Lambda 函数解析 DynamoDB 的 JSON 格式)
          <tbody id='67wDF'></tbody>
          <bdo id='67wDF'></bdo><ul id='67wDF'></ul>
          • <legend id='67wDF'><style id='67wDF'><dir id='67wDF'><q id='67wDF'></q></dir></style></legend>

            <small id='67wDF'></small><noframes id='67wDF'>

              <i id='67wDF'><tr id='67wDF'><dt id='67wDF'><q id='67wDF'><span id='67wDF'><b id='67wDF'><form id='67wDF'><ins id='67wDF'></ins><ul id='67wDF'></ul><sub id='67wDF'></sub></form><legend id='67wDF'></legend><bdo id='67wDF'><pre id='67wDF'><center id='67wDF'></center></pre></bdo></b><th id='67wDF'></th></span></q></dt></tr></i><div id='67wDF'><tfoot id='67wDF'></tfoot><dl id='67wDF'><fieldset id='67wDF'></fieldset></dl></div>
                <tfoot id='67wDF'></tfoot>
                本文介绍了Python Lambda 函数解析 DynamoDB 的 JSON 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                为 dynamodb 流调用的 Python Lambda 函数具有 DynamoDB 格式的 JSON(包含 JSON 中的数据类型).我想将 DynamoDB JSON 转换为标准 JSON.PHP 和 nodejs 有 Marshaler 可以做到这一点.如果 Python 有类似或其他选项,请告诉我.

                Python Lambda function that gets invoked for a dynamodb stream has JSON that has DynamoDB format (contains the data types in JSON). I would like to covert DynamoDB JSON to standard JSON. PHP and nodejs have Marshaler that can do this. Please let me know if there are similar or other options for Python.

                DynamoDB_format = `{"feas":
                    {"M": {
                        "fea": {
                            "L": [
                                {
                                    "M": {
                                        "pre": {
                                            "N": "1"
                                        },
                                        "Li": {
                                            "N": "1"
                                        },
                                        "Fa": {
                                            "N": "0"
                                        },
                                        "Mo": {
                                            "N": "1"
                                        },
                                        "Ti": {
                                            "S": "20160618184156529"
                                        },
                                        "Fr": {
                                            "N": "4088682"
                                        }
                                    }
                                }
                                ]
                            }   
                        }
                    }
                }`
                

                推荐答案

                更新:现在有一个库:https://pypi.org/project/dynamodb-json/

                这是 indiangolfer 的回答的改进版本.虽然 @indiangolfer 的解决方案适用于该问题,但此改进版本可能对偶然发现此线程的其他人更有用.

                Here is an improved version of indiangolfer's answer. While @indiangolfer's solution works for the question, this improved version might be more useful for others who stumble upon this thread.

                def unmarshal_dynamodb_json(node):
                    data = dict({})
                    data['M'] = node
                    return _unmarshal_value(data)
                
                
                def _unmarshal_value(node):
                    if type(node) is not dict:
                        return node
                
                    for key, value in node.items():
                        # S – String - return string
                        # N – Number - return int or float (if includes '.')
                        # B – Binary - not handled
                        # BOOL – Boolean - return Bool
                        # NULL – Null - return None
                        # M – Map - return a dict
                        # L – List - return a list
                        # SS – String Set - not handled
                        # NN – Number Set - not handled
                        # BB – Binary Set - not handled
                        key = key.lower()
                        if key == 'bool':
                            return value
                        if key == 'null':
                            return None
                        if key == 's':
                            return value
                        if key == 'n':
                            if '.' in str(value):
                                return float(value)
                            return int(value)
                        if key in ['m', 'l']:
                            if key == 'm':
                                data = {}
                                for key1, value1 in value.items():
                                    if key1.lower() == 'l':
                                        data = [_unmarshal_value(n) for n in value1]
                                    else:
                                        if type(value1) is not dict:
                                            return _unmarshal_value(value)
                                        data[key1] = _unmarshal_value(value1)
                                return data
                            data = []
                            for item in value:
                                data.append(_unmarshal_value(item))
                            return data
                

                从以下几个方面进行改进:

                It is improved in the following ways:

                • 处理更多数据类型,包括以前未正确处理的列表

                • handles more data types, including lists, which were not handled correctly previously

                处理小写和大写键

                修复递归对象错误

                这篇关于Python Lambda 函数解析 DynamoDB 的 JSON 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                env: python: No such file or directory(env: python: 没有这样的文件或目录)
                How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)
              1. <tfoot id='kOVDH'></tfoot>

                    <tbody id='kOVDH'></tbody>
                  • <legend id='kOVDH'><style id='kOVDH'><dir id='kOVDH'><q id='kOVDH'></q></dir></style></legend>

                    <small id='kOVDH'></small><noframes id='kOVDH'>

                  • <i id='kOVDH'><tr id='kOVDH'><dt id='kOVDH'><q id='kOVDH'><span id='kOVDH'><b id='kOVDH'><form id='kOVDH'><ins id='kOVDH'></ins><ul id='kOVDH'></ul><sub id='kOVDH'></sub></form><legend id='kOVDH'></legend><bdo id='kOVDH'><pre id='kOVDH'><center id='kOVDH'></center></pre></bdo></b><th id='kOVDH'></th></span></q></dt></tr></i><div id='kOVDH'><tfoot id='kOVDH'></tfoot><dl id='kOVDH'><fieldset id='kOVDH'></fieldset></dl></div>

                        <bdo id='kOVDH'></bdo><ul id='kOVDH'></ul>