最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Update value in Nested Json file using Python - Stack Overflow

programmeradmin6浏览0评论

I have a json file with quite a number of nested levels. As part of our deployment from one environment to another, I would like to automate replacing the old domain with new domain value wherever it exists in the json file I have tried doing this using python, but struggling with doing an iterative approach. I can replace some of the domain values, but not the ones that are quite deep in the nested levels. Also the old domain value could be anywhere in the json file, even deeper level

Json file is as in screenshot

oldurl=''
newurl=''

json_key = 'url'
for layer in webmap.layers:
   for lyr in layer.layers:
        print(lyr[json_key])
            if oldurl in lyr[json_key]:
               print('Updating ' + lyr[json_key])            
               lyr[json_key] = lyr[json_key].replace(oldurl,newurl)

I have a json file with quite a number of nested levels. As part of our deployment from one environment to another, I would like to automate replacing the old domain with new domain value wherever it exists in the json file I have tried doing this using python, but struggling with doing an iterative approach. I can replace some of the domain values, but not the ones that are quite deep in the nested levels. Also the old domain value could be anywhere in the json file, even deeper level

Json file is as in screenshot

oldurl='https://olddomain.co.uk/arcgis'
newurl='https://newdomain.co.uk/server'

json_key = 'url'
for layer in webmap.layers:
   for lyr in layer.layers:
        print(lyr[json_key])
            if oldurl in lyr[json_key]:
               print('Updating ' + lyr[json_key])            
               lyr[json_key] = lyr[json_key].replace(oldurl,newurl)

Share Improve this question asked Mar 20 at 18:39 sunsilk85sunsilk85 211 gold badge1 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I think a recursive approach will work well here, since you don't know how deep the nesting goes. To do that you can do something along the lines of this:

import json

oldurl = "oldurl"
newurl = "newurl"


def update_urls(obj):
    if isinstance(obj, dict):
        return {k: update_urls(v) for k, v in obj.items()}
    elif isinstance(obj, list):
        return [update_urls(item) for item in obj]
    elif isinstance(obj, str):
        return obj.replace(oldurl, newurl) if oldurl in obj else obj
    return obj


with open("yourfile", "r", encoding="utf-8") as file:
    data = json.load(file)

updated_data = update_urls(data)

Alternatively, you can just load the json file as a string, and greedily replace all instances of oldurl with newurl. This may cause issues though, depending on exactly how the file is structured, so I wouldn't necessarily recommend this approach.

oldurl = "oldurl"
newurl = "newurl"

with open("file.json", "r", encoding="utf-8") as f:
    data = f.read()

data = data.replace(oldurl, newurl)
发布评论

评论列表(0)

  1. 暂无评论