我正在用 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 上找到脚本,here(here 是代码)。
不幸的是,它没有记录;当时我并不真的相信其他人会使用它,我猜。
无论如何,如果您想尝试一下,请将脚本保存在您的工作目录中,然后使用以下内容:
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*')
不幸的是,正如我所说,脚本没有很好的文档记录,所以如果你想尝试它并需要更多信息,我很乐意提供它。
关于python - 在 Python 中更新复杂的 JSON 对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37074621/