-1

I'm working with a JSON file and I was wondering if there's a way to append a string to a list within the file. Here's an example of the JSON file I'm working with:

{"language": "['English', 'French']", "bank": 50}

I want to add the string "Spanish" to the "language" list. How can I do this?

Here's the code I've written so far, but I'm not sure how to modify it to achieve what I want:

import json

with open("example.json", "r") as jsonFile:
    data = json.load(jsonFile)

add list(data["language"]['Spanish'])

with open("example.json", "w") as jsonFile:
    json.dump(data, jsonFile)

How can I modify this code to achieve my goal?

5
  • data["language"].append('Spanish'), then save back to your file. Commented Feb 13, 2023 at 14:46
  • 1
    Are you sure that the ['English', 'French'] in your file is surrounded by double quotes? Because that seems an awkward, probably bad, choice of JSON structure. It also invalidates my suggestion above. Commented Feb 13, 2023 at 14:49
  • if i use " i have another error Commented Feb 13, 2023 at 14:55
  • Here is my json structure {"language": "['English', 'French']", "bank": 50} Commented Feb 13, 2023 at 14:56
  • 1
    Better to fix the input file to a more appropriate JSON format, then to fiddle around and doing weird things in code. Commented Feb 13, 2023 at 16:54

3 Answers 3

2

{"language": "['English', 'French']", "bank": 50}

Here the "language" keys hold a string rather than a list because of the " before [ and " after ]. To solve this, change the file to this:

{"language": ["English", "French"], "bank": 50}

Then use this code to append "Spanish" or any language from now on:

import json

with open("temp.json", "r") as f:
    data = json.load(f)

data["language"].append("Spanish")

with open("temp.json", "w") as f:
    json.dump(data, f)
Sign up to request clarification or add additional context in comments.

Comments

1
import json
import ast

with open("example.json", "r") as jsonFile:
    data = json.load(jsonFile)

#data={"language": "['English', 'French']", "bank": 50}

#take a variable e

e=ast.literal_eval(data['language'])

e.append('Spanish')

data['language']=e

print(data)
#{'language': ['English', 'French', 'Spanish'], 'bank': 50}

1 Comment

An explanation would be in order. E.g., what did you change? What is the idea/gist? From the Help Center: "...always explain why the solution you're presenting is appropriate and how it works". Please respond by editing (changing) your answer, not here in comments (**** without **** "Edit:", "Update:", or similar - the answer should appear as if it was written today).
0

In order to add to a Python list you should use the append() method.

Example:

import ast
ast.literal_eval(data["language"]).append("Spanish")

7 Comments

This give me an error: AttributeError: 'str' object has no attribute 'append'
See my second comment to the question.
@BatteTarte if that's the case of list in double quotes you could: import ast ast.literal_eval(data["language"]).append("Spanish")
This not work @slqq it just end the terminal and nothing happened
literal_eval: "This is specifically designed not to execute Python code ... But it is not free from attack: A relatively small input can lead to memory exhaustion or to C stack exhaustion, crashing the process. There is also the possibility for excessive CPU consumption denial of service on some inputs. Calling it on untrusted data is thus not recommended."
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.