在Python中更新复杂的JSON对象

勘误表

我正在用Python(v3.5)抓取某种复杂的MongoDB文档,我应该更新其中的一些值,这些值散布在对象周围,结构中没有特定的模式,然后将其保存回另一个MongoDB集合中。该对象如下所示:

# after json.loads(mongo_db_document) my dict looks like this
notification = {
    '_id': '570f934f45213b0d14b1256f',
    'key': 'receipt',
    'label': 'Delivery Receipt',
    'version': '0.0.1',
    'active': True,
    'children': [
        {
            'key': 'started',
            'label': 'Started',
            'children': [
                'date',
                'time',
                'offset'
            ]
        },
        {
            'key': 'stop',
            'label': 'Ended',
            'children': [
                'date',
                'time',
                'offset'
            ]
        },
        {
            'label': '1. Particulars',
            'template': 'formGroup',
            'children': [
                {
                    'children': [
                        {
                            'key': 'name',
                            'label': '2.1 Name',
                            'value': '********** THIS SHOULD BE UPDATED **********',
                            'readonly': 'true'
                        },
                        {
                            'key': 'ims_id',
                            'label': '2.2 IMS Number',
                            'value': '********** THIS SHOULD BE UPDATED **********',
                            'readonly': 'true'
                        }
                    ]
                },
                {
                    'children': [
                        {
                            'key': 'type',
                            'readonly': '********** THIS SHOULD BE UPDATED **********',
                            'label': '2.3 Type',
                            'options': [
                                {
                                    'label': 'Passenger',
                                    'value': 'A37'
                                },
                                {
                                    'label': 'Cargo',
                                    'value': 'A35'
                                },
                                {
                                    'label': 'Other',
                                    'value': '********** THIS SHOULD BE UPDATED **********'
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            'template': 'formGroup',
            'key': 'waste',
            'label': '3. Waste',
            'children': [
                {
                    'label': 'Waste',
                    'children': [
                        {
                            'label': 'Plastics',
                            'key': 'A',
                            'inputType': 'number',
                            'inputAttributes': {
                                'min': 0
                            },
                            'value': '********** THIS SHOULD BE UPDATED **********'
                        },
                        {
                            'label': 'B. Oil',
                            'key': 'B',
                            'inputType': 'number',
                            'inputAttributes': {
                                'min': 0
                            },
                            'value': '********** THIS SHOULD BE UPDATED **********'
                        },
                        {
                            'label': 'C. Operational',
                            'key': 'C',
                            'inputType': 'number',
                            'inputAttributes': {
                                'min': 0
                            },
                            'value': '********** THIS SHOULD BE UPDATED **********'
                        }
                    ]
                }
            ]
        },
        {
            'template': 'formRow',
            'children': [
                'empty',
                'signature'
            ]
        }
    ],
    'filter': {
        'timestamp_of_record': [
            'date',
            'time',
            'offset'
        ]
    }
}

我最初的想法是将占位符(如$var_name)放在需要更新值的位置,并用Python的字符串加载字符串string.Template,但是由于某种原因,这种方法很不幸地破坏了同一个MongoDB文档的其他用户的工作。

是否有解决方案可以简单地修改此类对象而无需“硬编码”路径来查找我需要更新的值?

罗伯托

有几年前我写的这个小脚本-我用它来查找一些非常长且令人烦恼的JSON中的条目。诚然,它并不漂亮,但也许对您有用。
您可以在此处的Bitbucket上找到脚本此处是代码)。不幸的是,它没有记录在案。我猜当时我还没有真正相信其他人会使用它。
无论如何,如果您想尝试一下,请将脚本保存在工作目录中,然后使用以下命令:

from RecursiveSearch import Retriever

def alter_data(json_data, key, original, newval):
    '''
    Alter *all* values of said keys
    '''
    retr = Retriever(json_data)
    for item_no, item in enumerate(retr.__track__(key)): # i.e. all 'value'
        # Pick parent objects with a last element False in the __track__() result,
        # indicating that `key` is either a dict key or a set element
        if not item[-1]: 
            parent = retr.get_parent(key, item_no)
            try:
                if parent[key] == original:
                    parent[key] = newval
            except TypeError:
                # It's a set, this is not the key you're looking for
                pass

if __name__ == '__main__':
    alter_data(notification, key='value', 
               original = '********** THIS SHOULD BE UPDATED **********',
               newval = '*UPDATED*')

不幸的是,正如我所说的那样,该脚本没有得到很好的文档说明,因此,如果您想尝试一下并需要更多信息,我将很乐意提供它。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章