My Jason Data looks like this:
{
"componentId": "SD1:1100047938",
"componentType": "Device",
"name": "WR50MS15-7938 (WR 33)",
"product": "SB 5000TL",
"productTagId": 9037,
"pvPower": 886,
"serial": "1100047938",
"specWhOutToday": 3.0909803921568626,
"specWhOutYesterday": 2.924313725490196,
"state": 307,
"totWhOutToday": 15764,
"totWhOutYesterday": 14914
}
How could i only extract:
- "state" to a separate file
- "pv-power" to a sperate file ?
Thanks !
CodePudding user response:
url = "https://172.16.63.100/api/v1/overview/Plant:1/devices?todayDate=2022-10-17T12:53:16.746Z"
payload={}
headers = {
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NjYwMTU0MzMsInN1YiI6Ik1iYWNobCIsInVpZCI6Ijc1YmNkNTM2LTFhOTYtNDQ4My05MjQxLWZkNjY5Zjk3M2Y5OCIsImV4cCI6MTY2NjAxOTAzM30.bMMAsD8iPrAXDp7fbnwYL3Y8lj4Ktok3tU9NHZkYq8s'
}
response = requests.request("GET", url, headers=headers, data=payload, verify=False)
#print(response.text)
data = json.loads(response.text)
with open('data.json', 'w') as f:
data = json.dump(data, f, indent = 2)
This is my Code for gathering the JSON Data.
I need to exctract the above mentioned Values.
CodePudding user response:
After you run data = json.loads(response.text)
, the json is loaded into your data
variable as a python dictionary.
So state = data.state
and pvPower = data.pvPower
should give the info you're after.
I'm not exactly sure what you want to do with that information, as far as extracting to a separate file. But, json.dump()
does output data to a json file.