Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to generate auto increment id in json using python?

What I have tried:

import json



emp_dic =[{
"name" : input("Enter employee name : "),
"emp_id": "2022-001" ,
"city" : input ("Enter city : "),
"experience" : int(input("Enter experience :")),
"ctc" : int(input("Enter CTC : ")),
"age" : int(input("Enter age : ")),
"contact" : int(input("Enter contact : "))
}]
with open ("emp.json","r") as file:
dic=json.load(file)
dic.append(emp_dic)
with open("emp.json","w") as file:
json.dump(dic, file , indent=4)
print(dic)
Posted
Updated 9-Jun-22 2:21am
Comments
Richard MacCutchan 9-Jun-22 8:35am    
What exactly are you trying to do? That is to say what value are you trying to increment?
Member 15667238 9-Jun-22 22:51pm    
I just want the emp_id auto generate serially in json file.

1 solution

Json doesn't have an autoincrement function - or indeed, any functions at all. It's a data storage and transfer specification, it doesn't have any computing elements of it's own.

If you want an autoincrementing number, you need to provide that in your code that reads and processes the data from the Json file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900