Hi All,
I have two questions. 1) How to save complex JSON from Payload to Device State simply 2) What is the best way to store device state as JSON and if MongoDB is the best fit for it
- I save device current state to Device State node as Individual Fields. Such approach requires a lot of Copy/Pasting activity and increases a chance for typos. I know that there are two other input options to set a device state called “JSON Template” that seems to be taking only manual entry values and “Payload Path” that seems to be taking only linear (n-1 depth) payload JSON. It does not seem to be handy for storing JSON-complex states of devices and moreover extracting it for processing later on.
Could you correct me please if I am missing some functionality of those input methods for a Device State node?
- It seems that Mongo DB is one the best alternatives for storing complex JSON device states. Is it correct? Are there any additional convenient approaches within losant to do it?
Thanks
Hi @Alexander_Kondrov,
I think this is a great question.
We like to think of all Device State data as being time-series data. Since time-series data is optimized for aggregations, that’s why they are single values (number, boolean, etc.). It’s tough to aggregate a complex JSON object over a specified amount of time.
But you do have options!
-
Because Device State can be a string, we’ve seen cases where other Losant Developers have encoded the JSON and stored that value as the attribute. But, this is not the best because you’ll always need to encode/decoded to use/store that value. Overall, we recommend saving the single values as this would allow you to aggregate and visualize them efficiently.
-
If the data is not time series, or you need data associated with a Losant Device or Experience Users, you can store it in a Data Table.
A note on the JSON Template, from this:
I know that there are two other input options to set a device state called “JSON Template” that seems to be taking only manual entry values and “Payload Path” that seems to be taking only linear (n-1 depth) payload JSON. It does not seem to be handy for storing JSON-complex states of devices and moreover extracting it for processing later on.
These, can allow you to set all of these in one place, and access nested values and arrays:
{
"attr1": "{{data.some.nested.value.[4].array.value}}",
"attr2": "{{data.value }}",
}
There are a ton of options here:
1 Like
Hi Taron,
Thank you for the reply. I had an idea of storing JSON partially in string attribute and now I will use it since there are already practical implementations. There an additional nuance: the array embedded in payload json may have a different size.
1 Like