update nested json object in python

Question:

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’

Asked By: teja

||

Answers:

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'}}}]}]}]}]}
Answered By: user56700
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.