Get a Json file that represents device(object) from a specific folder and generating kafka event

Viewed 32

I need make tool using Python that takes JSON file(device) from a folder(devices) and generates kafka events for the device.events topic. before generating event it has to check if device exists in folder.

I wrote this but i still dont know how to open file inside the folder.

import json

test_devices = r'''
{
    "data": ["{\"mac_address\": null, \"serial_number\": \"BRUCE03\", \"device_type\": \"STORAGE\", \"part_number\": \"STCHPRTffOM01\", \"extra_attributes\": [], \"platform_customer_id\": \"a44a5c7c65ae11eba916dc31c0b885\", \"application_customer_id\": \"4b232de87dcf11ebbe01ca32d32b6b77\"}"],
            "id": "b139937e-5107-4125-b9b0-d05d17ad2bea",
            "source":"CCS",
            "specversion":"1.0",
            "time": "2021-01-13T14:44:18.972181+00:00",
            "type":"`  `",
            "topic":"`  `"
}
'''

data = json.loads(test_devices)
print(type(test_devices))
print(data)
1 Answers

Kafka is an implementation detail and doesn't relate to your questions.

how to open file inside the folder

import json
with open("/path/to/device.json") as f:
  content = json.load(f)
  print(content)

has to check if device exists in folder

import os.path
print("Exists? :", os.path.exists("/path/to/device.json"))
Related