Home > Back-end >  How to specify JSON data
How to specify JSON data

Time:09-02

I'm wondering how I'm supposed to go about getting a single stream of data or rather, I'm trying to get the value that is given near the end of the script, specifically the text value for ModbusSerial.Port_1.COM.Power and I'm wondering how I might go about doing that. I don't know exactly how to write that code. I know it's something along the lines of

json.dumps(data12_json['MTConnectStreams']['Streams']['ComponentStream'], indent=4) 

but after that, I'm lost as how to further specify the path and get back my value. Help would be greatly appreciated.

>>> print(json.dumps(data12_json, indent=4))
{
    "MTConnectStreams": {
        "@xmlns": "urn:mtconnect.com:MTConnectStreams:1.1",
        "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "@xsi:schemaLocation": "urn:mtconnect.com:MTConnectStreams:1.1 http://www.mtconnect.org/schemas/MTConnectStreams_1.1.xsd",
        "Header": {
            "@creationTime": "2022-09-01T22:52:13",
            "@sender": "RevPi",
            "@instanceId": "637976619262601870",
            "@bufferSize": "10000",
            "@version": "4.0.22178.0",
            "@nextSequence": "190606",
            "@firstSequence": "79",
            "@lastSequence": "190606"
        },
        "Streams": {
            "DeviceStream": {
                "@uuid": "ModbusSerial",
                "@id": "ModbusSerial",
                "@name": "ModbusSerial",
                "Events": {
                    "Other": [
                        {
                            "@dataItemId": "ModbusSerial.Faulted",
                            "@sequence": "354",
                            "@timestamp": "2022-09-01T16:45:21",
                            "@name": "Faulted",
                            "#text": "False"
                        },
                        {
                            "@dataItemId": "ModbusSerial.FaultInfo",
                            "@sequence": "79",
                            "@timestamp": "2022-09-01T16:45:20",
                            "@name": "FaultInfo"
                        }
                    ]
                },
                "ComponentStream": [
                    {
                        "@component": "Component",
                        "@componentId": "ModbusSerial.Port_1",
                        "@name": "Port_1",
                        "Samples": {
                            "Other": [
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.BaudRate",
                                    "@sequence": "359",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "BaudRate",
                                    "#text": "19200"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.SamplePeriod",
                                    "@sequence": "360",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "SamplePeriod",
                                    "#text": "1000"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.BetweenModule",
                                    "@sequence": "361",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "BetweenModule",
                                    "#text": "300"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.ExceptionCounter",
                                    "@sequence": "363",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "ExceptionCounter",
                                    "#text": "0"
                                }
                            ]
                        },
                        "Events": {
                            "Other": [
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.Configured",
                                    "@sequence": "355",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "Configured",
                                    "#text": "True"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.IsConnected",
                                    "@sequence": "471",
                                    "@timestamp": "2022-09-01T16:45:23",
                                    "@name": "IsConnected",
                                    "#text": "True"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.Error",
                                    "@sequence": "357",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "Error",
                                    "#text": "False"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.ShowErrors",
                                    "@sequence": "358",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "ShowErrors",
                                    "#text": "False"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.PortName",
                                    "@sequence": "87",
                                    "@timestamp": "2022-09-01T16:45:20",
                                    "@name": "PortName",
                                    "#text": "/dev/ttyUSB0"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.ConnectionSettings",
                                    "@sequence": "469",
                                    "@timestamp": "2022-09-01T16:45:23",
                                    "@name": "ConnectionSettings",
                                    "#text": "/dev/ttyUSB0-(19200,8,None,One)"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.Status",
                                    "@sequence": "579",
                                    "@timestamp": "2022-09-01T16:45:24",
                                    "@name": "Status",
                                    "#text": "Read successful"
                                }
                            ]
                        }
                    },
                    {
                        "@component": "Component",
                        "@componentId": "ModbusSerial.Port_1.COM3",
                        "@name": "COM3",
                        "Samples": {
                            "Other": [
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.Address",
                                    "@sequence": "364",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "Address",
                                    "#text": "1"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.CommCounter",
                                    "@sequence": "190605",
                                    "@timestamp": "2022-09-01T18:52:13",
                                    "@name": "CommCounter",
                                    "#text": "7605"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.CommTime",
                                    "@sequence": "190604",
                                    "@timestamp": "2022-09-01T18:52:13",
                                    "@name": "CommTime",
                                    "#text": "124"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.CommFailedCounter",
                                    "@sequence": "370",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "CommFailedCounter",
                                    "#text": "0"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.HoldingRegister",
                                    "@sequence": "373",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "HoldingRegister",
                                    "#text": "0"
                                }
                            ]
                        },
                        "Events": {
                            "Other": [
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.Connected",
                                    "@sequence": "578",
                                    "@timestamp": "2022-09-01T16:45:24",
                                    "@name": "Connected",
                                    "#text": "True"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.CommFailed",
                                    "@sequence": "368",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "CommFailed",
                                    "#text": "False"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.CommLastTime",
                                    "@sequence": "190606",
                                    "@timestamp": "2022-09-01T18:52:13",
                                    "@name": "CommLastTime",
                                    "#text": "9/1/2022 10:52:13 PM"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.InputRegister",
                                    "@sequence": "372",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "InputRegister",
                                    "#text": "0"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.Template",
                                    "@sequence": "130",
                                    "@timestamp": "2022-09-01T16:45:20",
                                    "@name": "Template"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.Power",
                                    "@sequence": "190595",
                                    "@timestamp": "2022-09-01T18:52:13",
                                    "@name": "Power",
                                    "#text": "60.85923"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.Current_A",
                                    "@sequence": "190596",
                                    "@timestamp": "2022-09-01T18:52:13",
                                    "@name": "Current_A",
                                    "#text": "0.8454652"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.PFA",
                                    "@sequence": "190597",
                                    "@timestamp": "2022-09-01T18:52:13",
                                    "@name": "PFA",
                                    "#text": "0.595107"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.Avg_Phase_Voltage",
                                    "@sequence": "190603",
                                    "@timestamp": "2022-09-01T18:52:13",
                                    "@name": "Avg_Phase_Voltage",
                                    "#text": "120.8791"
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
}

CodePudding user response:

Your structure is pretty complex you need to write all the path down to where it can be

def search(data, key):
    for component in data['MTConnectStreams']['Streams']['DeviceStream']['ComponentStream']:
        for sample in component['Samples']['Other']:
            if sample['@dataItemId'] == key:
                return sample['#text']
        for sample in component['Events']['Other']:
            if sample['@dataItemId'] == key:
                return sample['#text']
key = "ModbusSerial.Port_1.COM3.Power"
data12_json = json.load(open("data.json"))
print(search(data12_json, key))  # 60.85923

CodePudding user response:

You'd probably want to pick first the dictionary object on the level where all event items are located (the most nested level that you can actually access).

It can be seen from the object structure that the "ModbusSerial.Port_1.COM3.Power" is not a key, but a value in one of the objects in the array. Same goes for all component events/streams on COM3. They are in the array located at:

data["MTConnectStreams"]["Streams"]["DeviceStream"]["ComponentStream"]

Take this object and you'll have an array of objects, specifically various component streams, from which one has a field "@componentId": "ModbusSerial.Port_1.COM3". A simple loop going through the array and picking the object containing that field would do the job.

Then, you would have an object, let's call it com3data. It also has nested objects in it. It has a field called Events in it, and that field also has Other field in it. Accessing it through com3data["Events"]["Other"] you will now have the array of objects, where one of them has the field "@dataItemId": "ModbusSerial.Port_1.COM3.Power".

Run a simple loop at com3data["Events"]["Other"], and find an object that has the above mentioned field with that value, and the object you get is exactly what you're looking for.

Hope I helped, at least a little bit.

CodePudding user response:

I think one area where you could be having problems is that the data is partially in list form inside the json as shown by the [] brackets instead of {}. This means that to acces for example the ModbusSerial.Port_1.COM.Power entry you would need to specify the following:

data12_json['MTConnectStreams']['Streams']['ComponentStream'][1]["Events"]["Other"][5]["@dataItemID"]
  • Related