Add attribute to json object python

I'm new to python and json files and I have a json file which has only one attribute and I want to update each object in the file by adding a new attribute, my json file looks like:

[
{"marka": "تويوتا"},
{"marka": "شيفروليه"},
{"marka": "نيسان"}
]

and I want it to be something like:

[
{"marka": "تويوتا" , "tag" : "MANF"},
{"marka": "شيفروليه" , "tag" : "MANF"},
{"marka": "نيسان" , "tag" : "MANF"}
]

I tried this code but it gives me an error:

with open["haraj_marka_arabic.json", "r"] as jsonFile:
     data = json.load[jsonFile]

tmp = data["tag"]
data["tag"] = "MANF"

with open["haraj_marka_arabic.json", "w"] as jsonFile:
    json.dump[data, jsonFile]

The error I had is:

TypeError: list indices must be integers, not str

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The full form of JSON is JavaScript Object Notation. It means that a script [executable] file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Python script. The text in JSON is done through quoted-string which contains the value in key-value mapping within { }. 
     

    Functions Used: 
     

    • json.loads[]: json.loads[] function is present in python built-in ‘json’ module. This function is used to parse the JSON string.
       

    Syntax: json.loads[json_string]
    Parameter: It takes JSON string as the parameter.
    Return type: It returns the python dictionary object. 
     

    • json.dumps[]: json.dumps[] function is present in python built-in ‘json’ module. This function is used to convert Python object into JSON string.
       

    Syntax: json.dumps[object]
    Parameter: It takes Python Object as the parameter.
    Return type: It returns the JSON string. 
     

    • update[]: This method updates the dictionary with elements from another dictionary object or from an iterable key/value pair.
       

    Syntax: dict.update[[other]]
    Parameters: Takes another dictionary or an iterable key/value pair.
    Return type: Returns None. 
     

    Example 1: Updating a JSON string.
      

    Python3

    import json

    x =  '{ "organization":"GeeksForGeeks",

            "city":"Noida",

            "country":"India"}'

    y = {"pin":110096}

    z = json.loads[x]

    z.update[y]

    print[json.dumps[z]]

    Output:
     

    {“pin”: 110096, “organization”: “GeeksForGeeks”, “country”: “India”, “city”: “Noida”} 
     

    Example 2: Updating a JSON file. Suppose the JSON file looks like this.
     

    We want to add another JSON data after emp_details. Below is the implementation.

    Python3

    import json

    def write_json[new_data, filename='data.json']:

        with open[filename,'r+'] as file:

            file_data = json.load[file]

            file_data["emp_details"].append[new_data]

            file.seek[0]

            json.dump[file_data, file, indent = 4]

    y = {"emp_name":"Nikhil",

         "email": "",

         "job_profile": "Full Time"

        }

    write_json[y]

    Output:
     


    How do you add a field to a JSON object in Python?

    How to add an element to a JSON object in Python.
    fruit_json = {"apple" : 1, "orange" : 2}.
    key = "banana".
    value = 3..
    fruit_json[key] = value..
    print[fruit_json].

    How do you push an element into a JSON object?

    Use Spread syntax to add JSON object to another JSON object in JavaScript. And use the push[] method to add a JSON array at end of an array.

    How do you update a JSON object in Python?

    How to update a JSON file in Python.
    a_file = open["sample_file.json", "r"].
    json_object = json. load[a_file].
    a_file. close[].
    print[json_object].
    json_object["d"] = 100..
    a_file = open["sample_file.json", "w"].
    json. dump[json_object, a_file].
    a_file. close[].

    How do I add a key value pair to an existing JSON object?

    To add a new key value pair in existing JSON object using JavaScript, we can parse the JSON string with JSON. parse , then add the key-value pairs we want, and then convert the object back to a JSON string with JSON. stringify . to parse s into an object with JSON.

    Chủ Đề