Home > Back-end >  How to change the value of a JSON list?
How to change the value of a JSON list?

Time:01-10

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!

CodePudding user response:

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'.

CodePudding user response:

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'

CodePudding user response:

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)
  •  Tags:  
  • Related