我有一个复杂的JSON
我想编辑多个值并再次发布 我想用不同的值替换每个“ SKU”
data = {
"products": [
{
"name": {
"ar": "PRODUCT_NAME",
"en": "PRODUCT_NAME"
},
"description": {
"ar": "PRODUCT_DESCRIPTION",
"en": "PRODUCT_DESCRIPTION"
},
"preparation_time_in_minutes": 4,
"image_path": "https://path/to/image.png",
"index": 0,
"pricing_type": 1,
"selling_type": 1,
"sku": "",
"created_at": "2018-05-08 12:53:55",
"updated_at": "2018-08-30 21:03:20",
"taxable": "true",
"is_combo": "false",
"calories": "null",
"is_active": "true",
"hid": "_a7g34337",
"barcode": "PRODUCT_BARCODE",
"category": {
"hid": "_697d1127"
},
"tags": [
{
"hid": "_16718997",
"relationship_data": {
"index": 0
}
}
],
"modifiers": [
{
"hid": "_567a437a",
"relationship_data": {
"index": 0,
"is_required": "false",
"minimum_options": 0,
"maximum_options": 100,
"excluded_options": [
"_567a437a"
]
}
}
],
"timed_events": [
{
"hid": "_567a437a"
}
],
"sizes": [
{
"name": {
"en": "SIZE_NAME",
"ar": "SIZE_NAME"
},
"price": 20,
"index": 0,
"created_at": "2018-09-04 14:46:24",
"updated_at": "2018-09-04 14:46:24",
"barcode": "SIZE_BARCODE",
"calories": 123,
"sku": "",
"cost": 0,
"has_fixed_cost": "false",
"hid": "_273ad637",
"costingType": "ingredients",
"ingredients": [
{
"hid": "_d741agg7",
"relationship_data": {
"is_optional": "false",
"quantity": 40,
"inactive_in_order_types": [
1,
2
]
}
}
],
"special_branch_prices": [
{
"branch_hid": "_d741agg7",
"price": 23
}
]
}
]
}
] }
我想找到第一个“ SKU”并替换其值
然后再次查找以找到其他“ SKU”并替换该值
我试图发现在每个请求中“ SKU”的位置都在改变
答案 0 :(得分:0)
对于第一个sku,请尝试以下操作:
data["products"][0]["sku"] = "What you want"
第二个尝试:
data["products"][0]["sizes"][0]["sku"] = "What you want x2"
您能看到这里发生了什么吗?我们首先需要告诉系统我们要读取数据。然后在数据内部,我们想要读取产品,该产品内部有一个列表,我们想要获取其第一个元素,即0。在此列表中,我们想要找到“ sku”,因为它在那里
现在,在同一列表内,还有一个名为“ sizes”的元素,该元素具有一个列表,其中一个元素是“ sku”。因此,我们也可以简单地访问它并进行更改。
答案 1 :(得分:0)
好吧,如果您希望将其自动化,并且每种产品的价值始终相同,则可以执行以下操作
def modify_value_from_json_by_key(json_input, lookup_key, new_value):
if isinstance(json_input, dict):
for k, v in json_input.items():
if k == lookup_key:
json_input[k] = new_value
else:
modify_value_from_json_by_key(v, lookup_key, new_value)
elif isinstance(json_input, list):
for item in json_input:
modify_value_from_json_by_key(item, lookup_key, new_value)
modify_value_from_json_by_key(data, "sku", "your_new_value")
这将输出:
data = {
"products": [{
"name": {
"ar": "PRODUCT_NAME",
"en": "PRODUCT_NAME"
},
"description": {
"ar": "PRODUCT_DESCRIPTION",
"en": "PRODUCT_DESCRIPTION"
},
"preparation_time_in_minutes": 4,
"image_path": "https://path/to/image.png",
"index": 0,
"pricing_type": 1,
"selling_type": 1,
"sku": "your_new_value",
"created_at": "2018-05-08 12:53:55",
"updated_at": "2018-08-30 21:03:20",
"taxable": "true",
"is_combo": "false",
"calories": "null",
"is_active": "true",
"hid": "_a7g34337",
"barcode": "PRODUCT_BARCODE",
"category": {
"hid": "_697d1127"
},
"tags": [
{
"hid": "_16718997",
"relationship_data": {
"index": 0
}
}
],
"modifiers": [
{
"hid": "_567a437a",
"relationship_data": {
"index": 0,
"is_required": "false",
"minimum_options": 0,
"maximum_options": 100,
"excluded_options": [
"_567a437a"
]
}
}
],
"timed_events": [
{
"hid": "_567a437a"
}
],
"sizes": [
{
"name": {
"en": "SIZE_NAME",
"ar": "SIZE_NAME"
},
"price": 20,
"index": 0,
"created_at": "2018-09-04 14:46:24",
"updated_at": "2018-09-04 14:46:24",
"barcode": "SIZE_BARCODE",
"calories": 123,
"sku": "your_new_value",
"cost": 0,
"has_fixed_cost": "false",
"hid": "_273ad637",
"costingType": "ingredients",
"ingredients": [
{
"hid": "_d741agg7",
"relationship_data": {
"is_optional": "false",
"quantity": 40,
"inactive_in_order_types": [
1,
2
]
}
}
],
"special_branch_prices": [
{
"branch_hid": "_d741agg7",
"price": 23
}
]
}
]
}
]}
如果列表中有更多产品,并且每个“产品”的价值必须不同,则可以尝试以下方法:
for product in data["products"]:
title = product.get('name').get("en")
if title == 'something_you_want_to_modify':
sku = 'product_sku_{}'.format(title)
modify_value_from_json_by_key(product, "sku", sku)
已编辑
for product in data['products']:
product['sku'] = 'your custom sku'
if 'sizes' in product:
product['sizes'][0]['sku'] = 'a different sku for you!' # if sizes only contains a single item in the list
# if sizes contains more items iterate through product['sizes'] and set each desires value
答案 2 :(得分:0)
我已经快速创建了一个基于递归的函数,该函数可通过python字典-json.loads(data)
进行迭代。
def replaceKey(data, key_to_replace = 'sku', string_to_insert = ''):
for key in data.keys():
if (type(data[key]) is list and type(data[key][0]) is dict):
for el in data[key]:
replaceKey(el)
elif (type(data[key]) is dict):
replaceKey(data[key])
elif (key == key_to_replace):
data[key_to_replace] = string_to_insert