在python中更新嵌套json对象

hkmswyz6  于 2023-01-14  发布在  Python
关注(0)|答案(1)|浏览(136)

我有一个json文件名input如下

{
  "abc": {
    "dbc": {
      "type": "string",
      "metadata": {
        "description": "Name of the  namespace"
      }
    },
    "fgh": {
      "type": "string",
      "metadata": {
        "description": "Name of the Topic"
      }
    }
  },
  "resources": [
    {
      "sku": {
        "name": "[parameters('sku')]"
      },
      "properties": {},
      "resources": [
        {
          "resources": [
            {
              "resources": [
                {
                  "properties": {
                    "filterType": "SqlFilter",
                    "sqlFilter": {
                      "sqlExpression": "HAI"
                    }
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

我想要“sqlExpression”:将“HAI”值替换为BYE,如下所示“sqlExpression”:“BYE”我希望python代码执行此操作,我尝试了以下代码,但输入[“资源”][0][“资源"][0]["资源"][0]["资源”][0][属性][0][sqlFilter][0][sqlExpression][0]=“BYE”无效

kcwpcxri

kcwpcxri1#

inp = {
  "abc": {
    "dbc": {
      "type": "string",
      "metadata": {
        "description": "Name of the  namespace"
      }
    },
    "fgh": {
      "type": "string",
      "metadata": {
        "description": "Name of the Topic"
      }
    }
  },
  "resources": [
    {
      "sku": {
        "name": "[parameters('sku')]"
      },
      "properties": {},
      "resources": [
        {
          "resources": [
            {
              "resources": [
                {
                  "properties": {
                    "filterType": "SqlFilter",
                    "sqlFilter": {
                      "sqlExpression": "HAI"
                    }
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

inp['resources'][0]['resources'][0]['resources'][0]['resources'][0]['properties']['sqlFilter']['sqlExpression']='BYE'

print(inp)

结果

{'abc': {'dbc': ...truncated... {'sqlExpression': 'BYE'}}}]}]}]}]}

相关问题