'Python - Search and Replace in JSON file

I'd like to replace the displayName field in python:

json = {
    "expand": "renderedFields,names,schema",
    "id": "743145",
    "self": "XXXXXXXXXXXX/api/2/issue/743145",
    "key": "TEST1234-1266",
    "fields": {
        "issuelinks": [],
        "assignee": {
            "self": "XXXXXXXXXXXX/api/2/user?username=XX.XX",
            "name": "[email protected]",
            "key": "JIRAUSER22031",
            "avatarUrls": {
                "48x48": "XXXXXXXXXXXXjira/secure/useravatar?avatarId=10122",
                "24x24": "XXXXXXXXXXXXjira/secure/useravatar?size=small&avatarId=10122",
                "16x16": "XXXXXXXXXXXXjira/secure/useravatar?size=xsmall&avatarId=10122",
                "32x32": "XXXXXXXXXXXXjira/secure/useravatar?size=medium&avatarId=10122"
            },
            "displayName": "[email protected]",
            "active": true,
            "timeZone": "Europe/London"
        },
        "components": [{
            "self": "XXXXXXXXXXXX/api/2/component/23015",
            "id": "23015",
            "name": "AWS_EC2",
            "description": "AWS_EC2"
        }]
    }
}

The way it is done currently is by a search and replace from a csv file.

code:

import json
import csv
import os
import requests

site = "https://xxxx.com/xxxxxx/rest/api/2/search?xxxx=project=UKTEST"

url = requests.get(site, auth=auth, )
url_json = mtd_list.json()
url_str = json.dumps(mtd_json)

with open("names.csv","r+") as data:

    # csv file
    csvInput = csv.reader(data)

    # Search and replace
    for row in csvInput:
        replacerow = url_str.replace(row[0], (row[1]),)
        url_str= replacerow

csv file contains:

[email protected] tbundy

How would I get the above to work but with the url_json?



Solution 1:[1]

You can update json like this:

url_json["displayName"] = ""

Whats the rule for changing displayName? Its not clear in your question.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jasmin Heifa