from time import sleep_ms
import json
import os
sleep_ms(200)
print("Hello, Pi Pico! We are now looking to read and write data to files...")
def read_json(file):
try:
with open(file) as fd:
return json.load(fd)
except OSError:
print("Unable to open the file with name: "+file)
def write_json(file, contents):
print("writing data to JSON file...")
try:
with open(file,"w") as fd:
json.dump(contents, fd)
fd.flush()
except OSError:
print("Unable to open the file with name: "+file)
def read_csv(file):
print("reading csv file")
print("\n-- START --\n")
print("\n== JSON File ==\n")
print("--> Reading file...")
json_contents=read_json("jsonfile.json")
print(f'Type of the JSON data object: {type(json_contents)}')
print("\nJSON file contents:")
print(json_contents)
print("\n--> Writing data to file... update contact number for father")
json_contents['contactNumbers'][0]['phone'] = '987-987-9876'
print("Updated contents")
print(json_contents)
write_json("jsonfile.json", json_contents)
json_contents=read_json("jsonfile.json")
print("\nJSON file contents after write:")
print(json_contents)
print("-- DONE --")