10-19-2019 09:48 AM
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
Solved! Go to Solution.
10-20-2019
09:47 AM
- last edited on
11-06-2024
04:19 PM
by
Content Cleaner
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.
10-21-2019 10:29 AM
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.
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
10-21-2019 10:54 AM
Looks like it works for me:
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?)
10-28-2019 04:19 AM
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:
Thanks a lot.
Michael
10-28-2019 09:02 AM
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.
10-28-2019 09:02 AM
My current solution. Is there a more elegant way?
10-28-2019 09:04 PM
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 string
"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.
09-11-2021 01:40 AM
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.