I know for fact that this question has been asked before. I looked at the following posts:
Python replace values in unknown structure JSON file
How to find and replace a part of a value in json file
https://pretagteam.com/question/how-to-replace-a-value-in-json-file-using-python
How to find and replace a part of a value in json file (Somehow this is what I am looking for but it does not fit my problem with the links being different and stored all under "latest")
and much more contributions, however I am still stuck.
I have a simple JSON structure like this:
{
"guild1": {
"latest": [
"link1",
"link2"
],
"channel": channel_here
},
"guild2": {
"latest": [
"link"
],
"channel": channel_here
}
}
I found a way to iterate over the entries and print the list in which the condition is found, for example:
# searching for link 1
["link1", "link2", "link3"] # here link 1 for example is found in one of the list entries
["link5"] -> This for example does not match what I am looking for, I can ignore this
If found, I simply want to replace it (just link1
) with another before defined value. I tried to iterate over the keys
of the found list but was not able to .replace
it or something else. The key which matches my search criteria is printed out, but I can't do anything else with it. Is it even possible to change the entry in a list that easy?
Mostly I also got the following error which I also looked up but did not get any did not get any wiser:
TypeError: string indices must be integers
Here is how I search for the list end the "entries":
if defined_link in data[searchloop]['latest']: # data is my JSON file, searchloop the for-loop
for key in data[searchloop]['latest']: # Get all the matching keys
if key == defined_link: # Get the key I am looking for
Maybe someone else can help me here!