LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to generate JSON with LabVIEW and trigger a PUT Request?

Solved!
Go to solution

Hello,

 

I am using a labview application to measure several values. With this data I would like to generate a json like the following. After generating the json I would like to trigger a PUT request.

 

{
    "topic": "com.xyz.abc",
    "headers": {},
    "path": "/",
    "value": {
        "attributes": {
            "sample": {
                "type": "STANDARD",
                "thickness": 9.2,
                "weight": 5.123,
            },
            "Mode": "USER_1",
            "Generator": {
                "mode": "10"
            },
            "noiseFilter": "MEDIAN",
            "Measuring": {
                "startOfMeasuring": "{{timestamp1}}",
                "endOfMeasuring": "{{timestamp2}}",
                "startOfDataRecording": "{{timestamp0}}",
                "endOfDataRecording": "{{timestamp3}}",
                "dataAggregation": {
                    "mode": "NEAREST",
                    "rawDataFrequency": 4.993,
                    "aggregatedFrequency": 1.0
                },
                "Readings": [
                    {
                        "timestamp": "{{timestamp0}}",
                        "PowerActive": false,
                        "temperatures": {
                            "rtd1": {
                                "value": {{temp0}}
                            },
                            "rtd2": {
                                "value": {{temp1}}
                            },
                            "rtd3": {
                                "value": {{temp2}}
                            },
                            "rtd4": {
                                "value": {{temp3}}
                            }
                        },
                        "power": {
                            "voltage":{{voltage0}},
                            "current":{{current0}},
                        }
                    }
                ]
            }
        }
    }
}

 Do you have any tipps or example how to generate json with labview and trigger put requests?

Thanks a lot for your help.

 

Best regards,

Michael

0 Kudos
Message 1 of 9
(4,460 Views)

You can generate JSON with the built-in Flatten To JSON function, but I suspect you might also find a number of toolkits if you prefer their interfaces.

As the help text hints at, the labels of elements in a cluster are used to create the labels of your JSON elements.

 

You can use the HTTP Client VIs to fire a PUT request. If you need to attach additional headers and so on, you can use the handle reference and the Open/Close Handle VIs.


GCentral
0 Kudos
Message 2 of 9
(4,395 Views)

Hello, many thanks for the reference to "flatten_to_json.vi". But I have a problem building the desired JSON. I can create the individual sections.

2019-10-21_17h24_36.png

 

When combining, however, I always get the hint "wrong data type". Where is my thought error to e.g. create this part?

"Readings": [
                    {
                        "timestamp": "{{timestamp0}}",
                        "PowerActive": false,
                        "temperatures": {
                            "rtd1": {
                                "value": {{temp0}}
                            },
                            "rtd2": {
                                "value": {{temp1}}
                            },
                            "rtd3": {
                                "value": {{temp2}}
                            },
                            "rtd4": {
                                "value": {{temp3}}
                            }
                        },
                        "power": {
                            "voltage":{{voltage0}},
                            "current":{{current0}},
                        }
                    }
                ]

Thank you very much for your help.

 

Best regards,

Michael

0 Kudos
Message 3 of 9
(4,367 Views)
Solution
Accepted by topic author MichaGue_01

Looks like it works for me:

Example_VI.png

 

You didn't show your attempt to create the combination, but I'd hazard a guess and say something didn't have a label. If you have some items (in a cluster) with labels, then all items in a cluster must have labels to use the Flatten to JSON node. This includes other clusters (which may have been your issue?)


GCentral
Message 4 of 9
(4,359 Views)

Is there a way to limit the number of decimal places of a double number when converting to JSON? I would like to limit them to 2 or 3 digits.

 

Current solution:

2019-10-28_10h15_06.png2019-10-28_10h15_18.png

 

Thanks a lot.

Michael

0 Kudos
Message 5 of 9
(4,323 Views)

So I originally thought that you might have some luck truncating the double, but this will only help you if you're willing to store it in some other format (e.g. string).

Otherwise, when you convert your manually truncated form back to double, you'll get the same problem (potentially not the same value, but very close).

 

This thread (and the post I link to in particular) discuss this issue in more detail.

 

If you want to store strings, of course the solution is simple - use Format to String and specify a precision, e.g. "%.2f" or "%.3f" in the format specifier.

This however, is not the same thing as a real numeric value.


GCentral
0 Kudos
Message 6 of 9
(4,304 Views)

My current solution. Is there a more elegant way?

 

2019-10-28_15h01_05.png2019-10-28_15h00_48.png

0 Kudos
Message 7 of 9
(4,302 Views)

You might find James Powell's JSONText addon interesting. It carries out some operation on doubles to seemingly check the place at which the floating-point errors will occur and then truncate the value before that.

It also then checks if it should remove trailing zeros, but you could use the same code to reduce the length of your value.

 

Essentially you're trying to produce a string, so what you're doing is the same idea. However, your implementation requires knowledge of the cluster you're flattening, at which point you could more easily just write it "by hand", as it were:

JSONtext version - manipulating the integer value at the right hand side allows changing the length of the stringJSONtext version - manipulating the integer value at the right hand side allows changing the length of the string

"by-hand" code. This requires no more information that what you already used in your replacements."by-hand" code. This requires no more information that what you already used in your replacements.

 

Others have implemented similar Variant-based flattenings, perhaps for similar reasons. You might find examples if you search for XML examples (rather than JSON). You'd probably end up doing quite a bit of work, but depending on how "feature complete" you needed to be, it might not be too bad.


GCentral
0 Kudos
Message 8 of 9
(4,280 Views)

How did you get pretty print in your output example with the standard flatten to JSON vi? My examples are always just a long string. 

0 Kudos
Message 9 of 9
(3,358 Views)