[Solved] How to manipulate a "string array" in json payload


#1

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 :slight_smile: so please help me out

thank’s in advance!


#2

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.


#3

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!


#4

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


Post Data to Google Sheets Particel
#5

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


#6

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.