【发布时间】:2019-02-18 17:57:42
【问题描述】:
我有以下 Json 文件:car_models.json
{
"name":"John",
"age":30,
"cars":
[
{
"car_model": "Mustang",
"car_brand": "Ford"
},
{
"car_model": "cx-5",
"car_brand": "Mazda"
}
]
}
我有另一个 json 文件 data_change.json,其中包含有关 jsonpath 及其值的详细信息:
{
"testcase_ID": "test_1A",
"description": "Some description",
"request_change_data": [
{
"element_path": "$.cars.[0].car_model",
"element_value": "focus"
}
]
}
我想读取 data_change.json 内容,使用此处的 element_path,解析 car_models.json 并将其值更新为 data_change.json 中的值。 如中,我想使用 jsonPath - $cars[0].car_model,通过 car_models.json 解析,并将 car_model 的值从 Mustang 更改为 focus。所以我更新后的 car_models.json 应该如下:
{
"name":"John",
"age":30,
"cars":
[
{
"car_model": "focus",
"car_brand": "Ford"
},
{
"car_model": "cx-5",
"car_brand": "Mazda"
}
]
}
如何在 python 中做到这一点?
【问题讨论】: