jsobj["a"]["b"]["e"].append({"f":var3, "g":var4, "h":var5})
jsobj["a"]["b"]["e"].append({"f":var6, "g":var7, "h":var8})
Answer from DrTyrsa on Stack Overflow
🌐
Python.org
discuss.python.org › python help
Unable to append data to Json array object with desired output - Python Help - Discussions on Python.org
January 18, 2023 - I’m tried getting help for same issue on stack-overflow but got no help or replies. I’m re-posting here with the hope that someone can please guide me as I’m unable to push the code to repository due to delay. My code import json import re from http.client import responses import vt import requests with open('/home/asad/Downloads/ssh-log-parser/ok.txt', 'r') as file: file = file.read() pattern = re.compile(r'\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}') ips = pattern.findall(file) unique_ips = lis...
Discussions

Append JSON array with another item using Python - Stack Overflow
I recently migrated to Python and cannot solve quite simple problem (using Django). A model has JSON field (PostgreSQL), lets say, for example, fruits. When updating an instance, I do it like this: More on stackoverflow.com
🌐 stackoverflow.com
Add values to JSON object in Python - Stack Overflow
I'm trying to use the json library in python to load the data and then add fields to the objects in the "accidents" array. More on stackoverflow.com
🌐 stackoverflow.com
python - How to add an element to a list? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
How do I add a nested JSON object to an existing object with python?
First: there's no such thing as a "JSON object". Json is just the method used to save and load data. Once it's loaded it's a standard python object. To do what you want you just load your file, make the changes you want with normal python syntax, and save it back, overwriting the old file. More on reddit.com
🌐 r/learnpython
5
4
November 22, 2022
Top answer
1 of 2
6

First, accidents is a dictionary, and you can't write to a dictionary; you just set values in it.

So, what you want is:

for accident in accidents:
    accident['Turn'] = 'right'

The thing you want to write out is the new JSON—after you've finished modifying the data, you can dump it back to a file.

Ideally you do this by writing to a new file, then moving it over the original:

with open('sanfrancisco_crashes_cp.json') as json_file:
    json_data = json.load(json_file)
accidents = json_data['accidents']
for accident in accidents:
    accident['Turn'] = 'right'
with tempfile.NamedTemporaryFile(dir='.', delete=False) as temp_file:
    json.dump(temp_file, json_data)
os.replace(temp_file.name, 'sanfrancisco_crashes_cp.json')

But you can do it in-place if you really want to:

# notice r+, not rw, and notice that we have to keep the file open
# by moving everything into the with statement
with open('sanfrancisco_crashes_cp.json', 'r+') as json_file:
    json_data = json.load(json_file)
    accidents = json_data['accidents']
    for accident in accidents:
        accident['Turn'] = 'right'
    # And we also have to move back to the start of the file to overwrite
    json_file.seek(0, 0)
    json.dump(json_file, json_data)
    json_file.truncate()

If you're wondering why you got the specific error you did:

In Python—unlike many other languages—assignments aren't expressions, they're statements, which have to go on a line all by themselves.

But keyword arguments inside a function call have a very similar syntax. For example, see that tempfile.NamedTemporaryFile(dir='.', delete=False) in my example code above.

So, Python is trying to interpret your accident['Turn'] = 'right' as if it were a keyword argument, with the keyword accident['Turn']. But keywords can only be actual words (well, identifiers), not arbitrary expressions. So its attempt to interpret your code fails, and you get an error saying keyword can't be an expression.

2 of 2
0

I solved with that :

with open('sanfrancisco_crashes_cp.json') as json_file:
        json_data = json.load(json_file)

        accidents = json_data['accidents']
        for accident in accidents:
            accident['Turn'] = 'right'

with open('sanfrancisco_crashes_cp.json', "w") as f:
        json.dump(json_data, f)
🌐
Finxter
blog.finxter.com › home › learn python blog › how to append data to a json file in python? [+video]
How to Append Data to a JSON File in Python? [+Video] - Be on the Right Side of Change
June 1, 2022 - Problem Formulation Given a JSON object stored in a file named "your_file.json" such as a list of dictionaries. 💬 How to append data such as a new dictionary to it? # File "your_file.json" (BEFORE) [{"alice": 24, "bob": 27}] # New entry: {"carl": 33} # File "your_file.json" (AFTER) [{"alice": 24, "bob": 27}, {"carl": 33}] Method 1: ... Read more
Find elsewhere
🌐
Tech With Tech
techwithtech.com › home › json object vs. json array explained with python
JSON Object vs. JSON Array Explained With Python - Tech With Tech
November 6, 2022 - When deserialized, the JSON array becomes a Python list. You can refer to the elements of this list by index. The JSON array here represents the value of the battery field in the JSON object: batteries = js_car ["battery"] >> print(batteries[0]) ... You can also pass a JSON array at the highest level of a JSON file or string. Let’s add another car and see what happens:
🌐
Quora
quora.com › How-do-you-add-elements-to-a-JSON-Array
How to add elements to a JSON Array - Quora
Answer (1 of 3): Here’s how you add elements to a JSON array—let’s break it down like you’re sitting around a table with a laptop, trying to figure this out. So, first off, JSON arrays are those square-bracket lists of stuff, right? Like `["apple", "banana"]`. To add something, you ...
🌐
ReqBin
reqbin.com › json › python › uzykkick › json-array-example
Python | What is JSON Array?
JSON arrays can store different ... objects, and multidimensional arrays. The values of a JSON array are separated by commas. Array elements can be accessed by using the "[]" operator. Unlike dictionaries, where you can get the value by its key, in a JSON array, the array elements can only be accessed by their index. The following is an example of a JSON array with numbers. Below, you can find a list of JSON arrays with different data types. The Python code was ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › append-to-json-file-using-python
Append to JSON file using Python - GeeksforGeeks
July 3, 2025 - Syntax: json.loads(json_string) Parameter: It takes JSON string as the parameter. Return type: It returns the python dictionary object.
🌐
Processing
processing.org › reference › JSONArray_append_.html
JSONArray - append() / Reference / Processing.org
January 1, 2021 - Appends a new value to the JSONArray, increasing the array's length by one. New values may be of the following types: int, float, String, boolean, JSONObject,…
🌐
Reddit
reddit.com › r/learnpython › how do i add a nested json object to an existing object with python?
How do I add a nested JSON object to an existing object with python? : r/learnpython
November 22, 2022 - Once it's loaded it's a standard python object. To do what you want you just load your file, make the changes you want with normal python syntax, and save it back, overwriting the old file. ... I want to use python to modify an existing JSON file and add the wanted data in the correct location
🌐
Medium
kendhia.medium.com › appending-a-json-item-to-a-json-list-cf3cf056481a
Appending a JSON item to a JSON list | by Dhia Kennouche | Medium
January 25, 2019 - I started recently working on a Python project, I loved the language. But as a person coming from Java World, the uncertainty (I call it like this) in the use of variables and sometimes the ambiguity in its errors is killing me. Of course probably this is happening ‘cuz I’m still a newbie in this beautiful world. Now to go to our point. I needed to open a file that contains a JSON List , and append a JSON object to it.
🌐
HowToDoInJava
howtodoinjava.com › home › python json › python – append to json file
Python - Append to JSON File
December 9, 2022 - Learn to append JSON data into file in Python. To append, read the file to dict object, update the dict object and finally write the dict to the file.
🌐
Stack Overflow
stackoverflow.com › questions › 51862285 › how-to-make-an-array-at-the-objects-json-field › 51958855
python - How to make an array at the object's JSON field? - Stack Overflow
@property def get_coordinates_formatted(self): coordinates = self.address.split(sep=",") return [coordinates[0], coordinates[1]] ... class FactorySerializer(serializers.ModelSerializer): my_JSON_field = serializers.JSONField() class Meta: model = Factory fields = ['my_JSON_field']
🌐
JSON Schema
json-schema.org › understanding-json-schema › reference › array
JSON Schema - array
The unevaluatedItems keyword is useful mainly when you want to add or disallow extra items to an array. unevaluatedItems applies to any values not evaluated by an items, prefixItems, or contains keyword. Just as unevaluatedProperties affects only properties in an object, unevaluatedItems affects ...
🌐
Quora
quora.com › How-will-you-pass-values-to-the-JSON-array-using-Python
How will you pass values to the JSON array using Python? - Quora
Answer (1 of 4): Your question is so wrong on so many things. I won’t really tell you to read the manual. You don’t do this in one pass you see. JSON stands for JavaScript Object Notation, which means, it is Javascript, it is not Python, C or anything else.