简体   繁体   English

将流数据转换为 json

[英]Transforming streaming data into a json

I have a streaming data coming from a source and I was able to capture few important variables like ID, Timestamp and Vital signs.我有一个来自源的流数据,我能够捕获一些重要的变量,如 ID、时间戳和生命体征。 What I am trying to do is create a json file object and unload it into a file system.我要做的是创建一个 json 文件对象并将其卸载到文件系统中。 This is one of the examples:这是示例之一:

for k in streaming:
   id = k[1]     ----------> 1
   timestamp = k[2]  ------> 1652304692
   vsigns = k[3]    -------> Nested dictionary 

This is how vsigns looks like这就是 vsigns 的样子

"vsigns":
    {
        "ECG":
        {
            "VPB_FREQ": "0",
            "STI": "+0.00"
        },
        "HR":
        {
            "HR_EKG": "87",
            "HR_PPG_1": "87",
            "HR_PULSED": "87"
        },
        "NIBP":
        {
            "NIBPS": "119",
            "NIBPD": "88",
            "NIBPM": "95"
        }
   }

And I want a json structure in the following format:我想要一个以下格式的 json 结构:

[{
    "id": "1",
    "ECG":
        {
           "timestamp": 1652304692,
            "VPB_FREQ": "0",
            "STI": "+0.00",
        }
},
{
    "id": "1",
    "HR":
        {
            "timestamp": 1652304692,
            "HR_EKG": "87",
            "HR_PPG_1": "87",
            "HR_PULSED": "87"
        }
        
},
{
    "id": "1",  
    "NIBP":
        {
            "timestamp": 1652304692,
            "NIBPS": "119",
            "NIBPD": "88",
            "NIBPM": "95"
        },
}]

I tried an approach but doesn't give me what I want.我尝试了一种方法,但没有给我想要的东西。 How do I get this right.我怎样才能做到这一点。

    for k in streaming:
       id = k[1]    
       timestamp = k[2]
       vsigns = k[3]
    vlist = []
    for k, v in vsigns.items():
       vdict = {"id": id,
                 k:{"timestamp":timestamp,
                     "vsigns": v}}
       vlist.append(vdict) 
print(vlist)

Output:输出:

[{
        "id": "1",
        "ECG":
        {
            "timestamp": 1652951054.0,
            "vsigns":
            {
                "VPB_FREQ": "0"
            }
        }
    },
    {
        "id": "1",
        "HR":
        {
            "timestamp": 1652951054.0,
            "vsigns":
            {
                "HR_EKG": "126",
                "HR_PPG_1": "127",
                "HR_PULSED": "127"
            }
        }
    },
    {
        "id": "1",
        "NIBP":
        {
            "timestamp": 1652951054.0,
            "vsigns":
            {
                "NIBPS": "95",
                "NIBPD": "46",
                "NIBPM": "62"
            }
        }
    }
}]

The following piece of code worked for me:以下代码对我有用:

for k in streaming:
       id = k[1]    
       timestamp = k[2]
       vsigns = k[3]
    vlist = []
    for k, v in vsigns.items():
       v['timestamp'] = epoch_time
       vdict = {"id": id,
                k:v}
       vlist.append(vdict)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM