0

I want to create a JSON file with requested projectname,customername,customeraddress,projectdescription parameters inside the ID folder which I have created.

I am new to Python, and I donot know how to do that. I tried searching a lot but couldnt find anything. Everything is running well until json.dump i.e. 8th last line

import os
import numpy as np
from flask import Flask,jsonify,request
import json

PROJECTS_DIR_NAME       = "Projects"
ID_DIR_NAME = 'ID_variable.npy'
SCRIPT_PATH = os.path.dirname(__file__)
PR_DIR          = os.path.join(SCRIPT_PATH,PROJECTS_DIR_NAME)
ID_DIR = os.path.join(PR_DIR,ID_DIR_NAME)

app = Flask(__name__)

@app.route('/initiate',methods =['POST'])
def initiate():
    path = PR_DIR
    f= ID_DIR
    try:
        ID_variable = np.load(ID_DIR)
        ID_variable = int(ID_variable)
        ID_variable =ID_variable+ 1
        np.save(f,ID_variable)
    except FileNotFoundError:
        np.save(f, 1)
        ID_variable=1
        
        
    os.chdir(PR_DIR)
    NewProjectID= 'ID'+ str(ID_variable)
    os.makedirs(NewProjectID)
    path2= path+'\\'+NewProjectID
    os.chdir(path2)
    projectname = request.args.get('projectname')
    customername = request.args.get('customername')
    customeraddress = request.args.get('customeraddress')
    projectdescription = request.args.get('projectdescription')
    json.dump({"Project Name": projectname} {"Customer Name": customername} {"Customer Address": customeraddress} {"Project Description" : projectdescription},path2)
    return jsonify({'ID': ID_variable})



if __name__ == '__main__':
    app.run(debug=True)

1 Answer 1

1

The code is not writing correct json and You need a file pointer for json.dump

The json you have is a list of jsons.

with open("your_file_name.json", "w") as f:
    json.dump([{"Project Name": projectname}, {"Customer Name": customername},{"Customer Address": customeraddress},{"Project Description" : projectdescription}],f)
Sign up to request clarification or add additional context in comments.

Comments

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.