1

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!

  • You say you tried and did several things, but you didn't provide any Python code, other than two literals for lists of strings - can you please provide a sample of your code, explain what happens and what you expected instead? – Grismar Jan 10 '22 at 01:06
  • @Grismar Sorry, was just editing the post. Have another look! –  Jan 10 '22 at 01:07
  • Am I understanding correctly that you'd like to find any occurrences of any of `["link1", "link2", "link3"]` and replace them with something else? What do you want to replace them with? Do you want them replaced in an and all 'guilds'? – Grismar Jan 10 '22 at 01:08
  • @Grismar I am already able to find all links that belong to my search criterion with this provided loop. I would like to replace these found links then with a new link, for this I simply took `"https://www.google.de/"` as a test, but of course nothing happened there. If found in every "guild", I want to replace it. –  Jan 10 '22 at 01:12

3 Answers3

1

This code would change one value in a list (here 'link1') to another:

L = ["link1", "link2", "link3"]
L[L.index('link1')] = 'someOtherValue'

After this you would see that L changed to ["someOtherValue", "link2", "link3"]

You first find at which index is 'link1', let's say you get back 3. So, you know you have to change the value of L[3]. Now it's simple. Do L[3] = 'someOtherValue'.

Kushal Kumar
  • 174
  • 1
  • 13
  • This works perfectly. Thanks! Do you know by any chance if indexing could possibly pull on the performance? –  Jan 10 '22 at 01:17
  • .index() function is not indexing but searching for a value in the list, which happens to be "link1" in the above example. And the time complexity is O(n) (most probably, only 99% sure). So, yes, searching a list takes a toll on performance. But depends on how big the list is (O(n)) and how many times would this function be called by users on the server. – Kushal Kumar Jan 10 '22 at 01:23
  • This code runs every 30 seconds, would need to test it a bit more to be sure it could work out. It would not have more than 10000 "links" though –  Jan 10 '22 at 01:31
0

I think you should try list.append(i) It will work I faced same problem

For example:

string = "Hello World!"

list = []

for i in string:

     list.append(string[i])

print(list)
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • `appending` in this case is adding a new value for me, if I understood your code in the direct way. I want to replace something in a list though. –  Jan 10 '22 at 01:30
0

Iterate over each guild in the json structure.

Iterate over each link in that guild's "latest" list.

If the link matches, assign that list item to something else.

for guild in jsonstructure:
    for i in range(len(jsonstructure[guild]['latest'])):
        if jsonstructure[guild]['latest'][i] == 'something':
            jsonstructure[guild]['latest'][i] = 'new thing'
John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • Hey John, thanks for your answer. This seems like a workable answer, too. Do you think If I have large lists, I will lose performance by looping over the JSON over and over again? Otherwise I would prefer this method as the answer by `Kushal Kumar` could take a lot of performance. –  Jan 10 '22 at 09:06