Home > database >  I'm trying to convert this file into json format & store it's output in another file
I'm trying to convert this file into json format & store it's output in another file

Time:04-20

{"Device_Value": 65, "Device_Parameter": "CPU", "Company_Id": "604fb5916f5231778e4d1738", "Device_Id": "5f28c42e-a225-4bc2-a810-26f55ffb9e15", "Date_Time": "2022-01-18 11:54:56.669410"}, {"Device_Value": 62, "Device_Parameter": "Memory", "Company_Id": "604fb591b643ad7b29fe0574", "Device_Id": "988c52af-9d8b-4a41-9e01-08c460595153", "Date_Time": "2022-01-18 11:54:56.669410"}, {"Device_Value": 7294, "Device_Parameter": "Revolutions", "Company_Id": "604fb5914d6fa25512d2b504", "Device_Id": "04c954d1-4925-4d74-9d20-6a779f473684", "Date_Time": "2022-01-18 11:54:56.669410"}, {"Device_Value": 37, "Device_Parameter": "Temperature", "Company_Id": "604fb59169789194d367ceea", "Device_Id": "34ea1925-69d6-42ed-9dbd-bfb76ec936c6", "Date_Time": "2022-01-18 11:54:56.669410"}, {"Device_Value": 44, "Device_Parameter": "Temperature", "Company_Id": "604fb591c2ff616b996e0f26", "Device_Id": "13b7c12a-040a-49e2-9838-0a9ac8609a42", "Date_Time": "2022-01-18 11:54:56.669410"}

CodePudding user response:

Your text already json format of each element But if you want convert from python dict to json, use json library

import json

text = [{"Device_Value": 65, "Device_Parameter": "CPU", "Company_Id": "604fb5916f5231778e4d1738",
         "Device_Id": "5f28c42e-a225-4bc2-a810-26f55ffb9e15", "Date_Time": "2022-01-18 11:54:56.669410"},
        {"Device_Value": 62, "Device_Parameter": "Memory", "Company_Id": "604fb591b643ad7b29fe0574",
         "Device_Id": "988c52af-9d8b-4a41-9e01-08c460595153", "Date_Time": "2022-01-18 11:54:56.669410"},
        {"Device_Value": 7294, "Device_Parameter": "Revolutions", "Company_Id": "604fb5914d6fa25512d2b504",
         "Device_Id": "04c954d1-4925-4d74-9d20-6a779f473684", "Date_Time": "2022-01-18 11:54:56.669410"},
        {"Device_Value": 37, "Device_Parameter": "Temperature", "Company_Id": "604fb59169789194d367ceea",
         "Device_Id": "34ea1925-69d6-42ed-9dbd-bfb76ec936c6", "Date_Time": "2022-01-18 11:54:56.669410"},
        {"Device_Value": 44, "Device_Parameter": "Temperature", "Company_Id": "604fb591c2ff616b996e0f26",
         "Device_Id": "13b7c12a-040a-49e2-9838-0a9ac8609a42", "Date_Time": "2022-01-18 11:54:56.669410"}]

with open('test.json', 'w') as file:
    json.dump(text, file)

CodePudding user response:

I think it's the only way

import json
data = [{"Device_Value": 65, "Device_Parameter": "CPU", "Company_Id": "604fb5916f5231778e4d1738", "Device_Id": "5f28c42e-a225-4bc2-a810-26f55ffb9e15", "Date_Time": "2022-01-18 11:54:56.669410"}, {"Device_Value": 62, "Device_Parameter": "Memory", "Company_Id": "604fb591b643ad7b29fe0574", "Device_Id": "988c52af-9d8b-4a41-9e01-08c460595153", "Date_Time": "2022-01-18 11:54:56.669410"}, {"Device_Value": 7294, "Device_Parameter": "Revolutions", "Company_Id": "604fb5914d6fa25512d2b504", "Device_Id": "04c954d1-4925-4d74-9d20-6a779f473684", "Date_Time": "2022-01-18 11:54:56.669410"}, {"Device_Value": 37, "Device_Parameter": "Temperature", "Company_Id": "604fb59169789194d367ceea", "Device_Id": "34ea1925-69d6-42ed-9dbd-bfb76ec936c6", "Date_Time": "2022-01-18 11:54:56.669410"}, {"Device_Value": 44, "Device_Parameter": "Temperature", "Company_Id": "604fb591c2ff616b996e0f26", "Device_Id": "13b7c12a-040a-49e2-9838-0a9ac8609a42", "Date_Time": "2022-01-18 11:54:56.669410"}]
    
with open(path_to_json, "w") as file:
       json.dump(data, file)
  • Related