OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Using Nested Objects in Python with objects named after variable (date)

  • Thread starter Thread starter john Wallace
  • Start date Start date
J

john Wallace

Guest
Im a begginer to coding and Im trying to create a short python script to reformat some JSON from a webdevice and create a new JSON file, eventually to put on a webserver for local access on my network. Im using some beginner python literature and a bit of goggling and so far I have :

Code:
import json
from datetime import date
import requests
import os

#create JSON structure as object names device

currentmonth = date.today().strftime("%Y%m%d")

device ={} #create empty ojects
month = {}
June24={}
Log_Cabin={}
Monthly_Total={}

month['June24'] = June24
device['Log_Cabin'] = Log_Cabin
Log_Cabin['month'] = month
month['Monthly Total'] = Monthly_Total


#capture JSON from shelly device
url = 'http://192.168.0.241/cm?cmnd=Status%208'
resp = requests.get(url=url)
data = resp.json() 

#assign values to variables
currentenergy = (data['StatusSNS']['ENERGY']['Power'])
totalenergyused = (data['StatusSNS']['ENERGY']['Total'])
filename = date.today().strftime("%B%y") + '.json'
key = date.today().strftime("%Y%m%d") #establish key as todays date
value = currentenergy # establish value as energyvariable
print(value)

try:
    with open(filename, 'r') as f:
        contents = f.read() or '{}'
    data = json.loads(contents)
except (FileNotFoundError, json.JSONDecodeError) as e:
    print("Error")

device["Log_Cabin"]["month"]['June24'][key] = value
print(value)
with open(filename, 'w+') as f:
        print(json.dumps(device), file=f)

This will create the basic structure on the file and update 1 value in the June24 part of the nested object. I would though like this part of the JSON object to be named dynamically as the month using datetime. Sorry if the question is a bit basic.

John
<p>Im a begginer to coding and Im trying to create a short python script to reformat some JSON from a webdevice and create a new JSON file, eventually to put on a webserver for local access on my network. Im using some beginner python literature and a bit of goggling and so far I have :</p>
<pre><code>import json
from datetime import date
import requests
import os

#create JSON structure as object names device

currentmonth = date.today().strftime("%Y%m%d")

device ={} #create empty ojects
month = {}
June24={}
Log_Cabin={}
Monthly_Total={}

month['June24'] = June24
device['Log_Cabin'] = Log_Cabin
Log_Cabin['month'] = month
month['Monthly Total'] = Monthly_Total


#capture JSON from shelly device
url = 'http://192.168.0.241/cm?cmnd=Status 8'
resp = requests.get(url=url)
data = resp.json()

#assign values to variables
currentenergy = (data['StatusSNS']['ENERGY']['Power'])
totalenergyused = (data['StatusSNS']['ENERGY']['Total'])
filename = date.today().strftime("%B%y") + '.json'
key = date.today().strftime("%Y%m%d") #establish key as todays date
value = currentenergy # establish value as energyvariable
print(value)

try:
with open(filename, 'r') as f:
contents = f.read() or '{}'
data = json.loads(contents)
except (FileNotFoundError, json.JSONDecodeError) as e:
print("Error")

device["Log_Cabin"]["month"]['June24'][key] = value
print(value)
with open(filename, 'w+') as f:
print(json.dumps(device), file=f)
</code></pre>
<p>This will create the basic structure on the file and update 1 value in the June24 part of the nested object. I would though like this part of the JSON object to be named dynamically as the month using datetime. Sorry if the question is a bit basic.</p>
<p>John</p>
 

Latest posts

Online statistics

Members online
0
Guests online
6
Total visitors
6
Top