skip to Main Content

I have the following JSON file named as info.Json:

{
    "jobs": [
        
        {
            "type": "Account",
            "min": 5,
            "max": 15,
            "arguments": {
                "inix": 48,
                "load": "3.081579e+07",
                "overload": "6.508037e+07"

            },
            "attributes": {
                "name": "emp_1",
                "priority_type": "low",
                "dependency": "-"
            }
        },
        {
            "type": "detect",
            "min": 5,
            "max": 13,
            "arguments": {
                "inix": 748,
                "load": "3.081579e+07",
                "overload": "6.508037e+07"

            },
            "attributes": {
                "name": "emp_2",
                "priority_type": "high",
                "dependency": "-"
            }
        },
         {
            "type": "Account",
            "min": 5,
            "max": 15,
            "arguments": {
                "inix": 48,
                "load": "3.081579e+07",
                "overload": "6.508037e+07"

            },
            "attributes": {
                "name": "emp_3",
                "priority_type": "low",
                "dependency": "-"
            }
        }
      
        
    ]
}

I have tried to write the following python code, to take a value of dependency from csv file and write it rather than the "-". What I need is just replace each occaurence of "-" with job_dependency value which read from csv file.

path='/load/statistics.csv'

for repeat in range(repeats):
        job_statistics = pd.read_csv(path)
        job_dependency = job_statistics['dependency'][repeat]
        print(job_exe_time)
        temp["attributes"]["dependency"]= f"{job_dependency}"

        with open("data_inv_jobs50.json", "wt") as fout:
                for line in file_data:
                         fout.write(line.replace("-","{job_exe_time}"))

I need to replace the value of "dependency": "-" with "dependency": "value from csv" , but my code above not working well .. any suggestion ?

2

Answers


  1. I came up with this:

    import json
    
    if __name__ == "__main__":
        # Retrieving the dependency value
        value_from_csv = 150
        # Opening the json file
        with open("info.json", "r", encoding="utf-8") as file:
            json_dict = json.load(file)
        # Modifying the json object
        for i in range(len(json_dict["jobs"])):
            json_dict["jobs"][i]["attributes"]["dependency"] = value_from_csv
        # Re-writing the object in a json file
        with open("info2.json", "w", encoding="utf-8") as file:
            json.dump(json_dict, file, indent=4)
    

    Here I asssumed there was only one dependency value. In this case, you can (and should) retrieve this value first, before entering the loop you will use to modify the content of your file.

    Login or Signup to reply.
  2. You can use the following script to replace the ‘-‘ value with your desired value.

    for item in dic['jobs']:
    ...:     item['attributes']['dependency'] = value_from_csv
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search