Hey Guys,
I have a question related to the “array”?! function. I want to extract to values of an array (but it actually is a string) - or I don’t know if this is the right word, it’s just a chain of values.
The part of the array looks like this:
…
“data”:“[‘open’, ‘OK’, ‘123’]”
…
now I want to get the both values seperated - but not by the value “open”,“OK” or “123” but by the index of the values. I wanted to use them seperated. So what I want to get is basically:
“data”:“[‘open’, ‘OK’, ‘123’]”
“status1”: “open”
“status2”:“OK”
“value”: “123”
So basically I want to know how I can seperate values that are in a chain, seperated by colons.
I could manipulate the input, so it could look like this
“data”:“open, OK, 123”
or this
“data”:“[open], [OK], [123]”
but I don’t know which is the right way so please help me out
thank’s in advance!
I imagine that you can write a Function block to parse this (or the new Loop/Array blocks), but the JSON decode block is one easy solution depending on your constraints. To be able to use this block, you need to send your data in as a JSON string, like this:
"{\"status1\":\"open\", \"status2\":\"OK\", \"value\":123}"
The JSON decode block will take each key/value pair and make a new payload object. What device/service are you sending the information from? They might already have a JSON encode/decode library you can use.
1 Like
thank you!
It worked out perfectly, I am using the Photon currently. I could send every value seperated but wanted a more “payload” saving option and also a smarter one, that could be integrated on other devices e.g. a ESP8266.
This is perfectly fine for my aims thank you!
1 Like
Good afternoon,
I am trying to do a similar thing as the above but I am getting an error on the debug. “InvalidJasonError”. Source did not contain valid JSON: container.jsonString
I am using a Particle Electron. My data is structured and published as follows.
snprintf(publishStateString, sizeof(publishStateString), "{ \"cf\":\"%.2f\", \"v\":\"%.1f\" }", flow, fuel.getSoC() );
Particle.publish("EF", publishStateString, 60, PRIVATE, NO_ACK);
I am trying the Json decode because I can not separate the values from the names, I have tried a number of different methods but keep coming up short. Then I found this topic and it looks like my solutions. Just can’t seem to get it right. Any help would be appreciated.
Here is how the date looks on losant debug.
Losant Screen Shot with JSON decoder settings.
Best.
Tom
Hi Tom,
In the JSON Decode configuration under the field Payload Path to Source String
you have container.jsonString
, which does not look like it’s on your payload, and thus getting an InvalidJsonError
. I believe you want to change that field to the path data.data
, if you want to decode the string, “{ “cf”:”-9.49", “v”:“42.0” }". And then you should see the decoded version of that at container.jsonString
.
Hope that helps,
Erin
1 Like
Erin!!! You have saved me from myself yet again!!!
Thank you so much. That worked perfectly.
For any other people new to Losant like me, I had to change my “Device State” node to the following.
Changed “Data Method” to “Payload Path”
Changed " State Data Payload Path" to “container.jsonString”
See the attached screenshot below.
Thank you again Erin, your the best.
Ok. I am also working with JSON payload, but trying to send it from experience Workflow to the Web Page. As far as I digged in, "
mark that Workflow JSON Encode Node puts to pageData is replaced with \"
expression. The latter qoute mark substitution drives crazy my javascript (syntax error) in the page, i.e. javascript is not loading.
Example is below:
My original array in Debug panel:
After Workflow JSON ENCODE NODE:
In PageData:
Once delivered to the page:
Could you advise how to overcome this problem and read array from JSON with Javascript on the page?
Hi @Alexander_Kondrov,
I am trying to replicate the "
character replacement you are seeing, but have not been successful. In my experience workflow, I have passed a JSON Encoded array to pageData, the original array and encoded array look similar to your first two arrays:
When I use the encoded array on the page, however, the quotes are not replaced with &qout;
:
I have a suspicion your problem could be solved by escaping HTML, but if not, please let me know how I can replicate the format you are seeing
Thanks!
Julia
Julia,
could you provide the code from the page you use to read JSON received from the Workflow in {{pageData}}?
@Alexander_Kondrov-
Mine is not the most beautiful of implementations, but I replaced “Sign In” on the Kanarra Experience with my array value just to see how it would be displayed in the HTML:
I was able to get the JSON Encoded format on the page with {{jsonEncode pageData.encodedArray}}
, but saw no "
characters.
Thanks!
Julia
Thank you Julia. I didn’t know about such jsonEncode command. After searching the forum a bit for a syntax and finding the example posted earlier by @anaptfox, I resolved my issue. Good news. This is a piece that I just copy-pasted.
<script>
var pageData = {{jsonEncode pageData}}
console.log(pageData)
</script>
Hey @Alexander_Kondrov,
I highly recommend taking a browse through this doc:
Those helpers are useful all throughout Losant and there are some really helpful ones in there.
Thank you for the advice. I took a look and tried a simple example with {{add pageData 4}} where pageData is an integer. I am still not sure how much applicable it is for FrontEnd Custom Pages since Workflow allows to publish only one variable “pageData”. In other words, from my point of view the most practical is to send data in JSON as “pageData” and use jsonEncode
command as recommended wrote for multiple variable extraction.
1 Like