0

I want to get the data of json folders using Python 3.7.2.

This is my json file:

{"device": 
  [
    {"name": "device1", "status": 0},
    {"name": "device2", "status": 1},
    {"name": "device2", "status": 1}
  ]
}

For my project I need following variables:

  • variable of all devices gathered in a list:

     devices = ["device1", "device1", "device1"]  
    
  • status of devices saved in different variables:

     status_device1 = 0
     status_device2 = 1
     status_device3 = 1
    

It is possible to change the structure of the json file, but it has to be only one json file, so I need something like:

jdevices = getDevices(json_data)             | Output: {'device1', 'device2', 'device3'}

and:

jstatus = getStatus(json_data, "device1")    | Output: 0 
JanF
  • 117
  • 2
  • 10
  • 1
    what you have tried? – Debendra Jan 29 '20 at 14:36
  • You should not want _Variable names_ that are read from data, like JSON. If your code will only deal with these 3 devieces, and you know it in advance, that is ok. Otherwise, you should have a dictionary with the status devices and a key for each device in this dictionary. – jsbueno Jan 29 '20 at 14:57
  • What is the issue, exactly? Have you tried anything, done any research? I agree with @jsbueno, this sounds a bit like a case of the [XY Problem](https://meta.stackexchange.com/q/66377/628382). – AMC Jan 29 '20 at 18:51

3 Answers3

2
import json

class DeviceHelper:

    def __init__(self, json_file):
        """open json file and load json content"""
        with open(json_file, 'rb') as file:
            self.device_info = json.load(file)

    def get_device_status(self, name):
        """loop over the divices list and compare"""
        for device in self.device_info.get('device'):
            if device.get('name') == name:
                return device.get('status')
        return 'Lol no device found'

    def get_devices(self):
        return [device.get('name') for device in self.device_info.get('device')]

# path to json file
device_info = DeviceHelper('devices.json')

device_info.get_device_status('device2')
>>> 1
device_info.get_devices()
>>> ['device1', 'device2', 'device2']
Debendra
  • 1,132
  • 11
  • 22
  • Thanks for helping, but your solution only works fot device1... Is there a way to rewrite the code? – JanF Jan 29 '20 at 16:22
1

python has built-in support for JSON files, and it also has a very handy data structure named dictionary which works brilliantly with JSON.

You'll basically need to do this:

  • tell python how to interact with JSON files: import json
  • load your data: data = jason.loads("path_to_file")
  • do whatever you'd want with the data, e.g. iterate over it:

for x in data: print(f"x: {x}, data[x]: {data[x]}")

M.H. Tajaddini
  • 776
  • 4
  • 20
0

You can do this in pandas

import pandas as pd
jsn = {"device": [
    {"name": "device1", "status": 0},
    {"name": "device2", "status": 1},
    {"name": "device2", "status": 1}]}

df = pd.DataFrame(jsn['device'])

      name  status
0  device1       0
1  device2       1
2  device2       1
devices = df['name'].tolist()
status = df['status'].tolist()
Kenan
  • 13,156
  • 8
  • 43
  • 50