JSON CRUD OPERATION IN PYTHON
What is crud opration?
It means the following operations:
- C- CREATE
- R- READ
- U- UPDATE
- D- DELETE
Now, taking the JSON it means JavaScript Object Notation which is a lightweight data-interchange format used to send data between computers and it is language independent.
It is written in the directory format.
Example of JSON
{
"name" :"Sam",
"age" :30,
"gender":male
}
Python has a built-in package called json
, which can be used to work with JSON data.
import json
Then let us take an example program for performinga crud operation, At first, let us create a json file and then store the data in that file.
FILEPATH = 'city.json'def get_json_data(): with open(FILEPATH) as json_file: data = json.load(json_file)return data
for storage :
def store_json_data(data): with open(FILEPATH, 'w') as outfile: json.dump(data, outfile)
Now let us have the main function for passing the data to other functions, for the main function we will name it as startpy()
In that, we will first declare the data and passes to create a function
from pprint import pprintdef startpy(): print("CRUD started!") # CRUD: Add City city = "Madurai" state = "Tamilnadu" add_city(city, state) # # CRUD: READ all cities = get_all_cities() pprint(cities)
Then in the create function for adding one data in the file.
# CRUD: CREATEdef add_city(city, state): current_data = { "name" : city, "state" : state } data = get_json_data() # print(data) data[city] = current_data store_json_data(data)
For Reading single data from the file the function is :
In the main function, we call a particular data
def startpy():# CRUD: READ single city_name = 'Waterloo' single_city = get_single_city(city_name) print(single_city)
In the read single data function
# CRUD: READ Singledef get_single_city(city_name): data = get_json_data() if(city_name in data): return data[city_name] # print(data)return None
The main function, data passing to Update function :
def startpy(): # CRUD: UPDATE city_name = 'Madurai' city_state = 'Tamilnadu' update_single_city(city_name, city_state)
Update function :
def update_single_city(city_name, city_state): data = get_json_data() if(city_name in data): city_data = { 'name' : city_name, 'state' : city_state } data[city_name] = city_data store_json_data(data)
Delete function in the main function :
def startpy():# CRUD: DELETE Single city_name = 'Montreal' delete_single_city(city_name)
Delete function for single data :
def delete_single_city(city_name): data = get_json_data() if(city_name in data): data.pop(city_name) store_json_data(data)
Delete function in the main function for all the data method 1:
def startpy():# CRUD: DELETE All start_time = time.time() delete_all_cities() executionTime = (time.time() - start_time) print('[delete_all_cities]Execution time in seconds: ' + str(executionTime))
Delete function for all the data :
def delete_all_cities():# get all keys and pop each data = get_json_data() city_list = list(data.keys()) for current_city in city_list: print(current_city) data.pop(current_city) store_json_data(data)
Effective delete function in the main for all the data:
import timedef startpy():# # # CRUD: DELETE All Efficient start_time = time.time() delete_all_cities_efficient() executionTime = (time.time() - start_time) print('[delete_all_cities_efficient]Execution time in seconds: ' + str(executionTime))
Effective delete function:
def delete_all_cities_efficient(): current_dict = {} store_json_data(current_dict)
For adding multi cites we can use faker methods by importing the faker method in the program
from faker import Fakerfake = Faker()
In the main function for multi adding cites:
def startpy():# # CRUD: Add multiple cities add_multiple_cities_test()
Multi cities adding function:
def add_multiple_cities_test(): Faker.seed(0) for c_index in range(1000): current_city = fake.city() current_state = fake.state() # print(current_city, current_state) add_city(current_city, current_state) print(f'{c_index} Added: ', current_city, current_state)
Conclusion:
All the functions are combined here you can refer to this link