Home > Blockchain >  update nested json object in python
update nested json object in python

Time:01-10

I have a json file name input which as follows

{
  "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"
                    }
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}                                                    

I want "sqlExpression": "HAI" value to be replaced with BYE as below "sqlExpression": "BYE" I want python code to do it, I tried the below code but not working input['resources'][0]['resources'][0]['resources'][0]['resources'][0][properties][0][sqlFilter][0][sqlExpression][0]='BYE'

CodePudding user response:

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)

Result

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