Answer the question
In order to leave comments, you need to log in
How to use python to extract data from json advice from ZONT API service?
Good day !
Can you please tell me how to extract data from json file?
I have a test.json file, with an example content:
{
"ok": true,
"events": [
[
"GuardOn-1486253766", // id
1486253766, // время
"GuardOn", // тип
43.871066, // долгота
56.166916, // широта
null, // продолжительность
{ // данные
"reason": "fob", // причина постановки на охрану
"fob_number": 1 // номер брелока
},
false // тревожное?
],
...
]
}
1486253766
"GuardOn"
Answer the question
In order to leave comments, you need to log in
import json
from collections import namedtuple
input_json = '''
{
"ok": true,
"events": [
[
"GuardOn-1486253766",
1486253766,
"GuardOn",
43.871066,
56.166916,
null,
{
"reason": "fob",
"fob_number": 1
},
false
]
]
}
'''
input_data = json.loads(input_json)
Event = namedtuple(
"Event",
("id", "time", "event_type", "longitude",
"latitude", "duration", "data", "is_alarm")
)
events = input_data.get("events")
for event in events:
event_data = Event(*event)
print(event_data.time)
print(event_data.event_type)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question