Below is how my JSON file looks like, Here I am continuously watching this. I am watching this file because I want to retrieve the every new item appended to JSON array in Python.
I want to know what could be a better approach.
[
{key1:value1,key2:value2},
{key1:value1,key2:value2}
]
I would say that way could be a good one:
import json
# path to the JSON file
json_file_path = 'your_file.json'
# Read the JSON content from the file
with open(json_file_path, 'r') as file:
data = json.load(file)
# Check the data received is a list
if isinstance(data, list):
# Check that it's not empty
if data:
# Get last item
last_item = data[-1]
print(last_item)
else:
print("The JSON array is empty.")
else:
print("The JSON data is not in the expected format (list of dictionaries).")