Dear,
How can I parse JSON string and decode from Base64 and then visualize in mashup ?
I receive this string
{"applicationID":"1","applicationName":"temperature-humidity","deviceName":"TH_sensor","time":"2019-08-26T14:31:54.833027Z","location":{"latitude":0,"longitude":0,"altitude":0}}],"data":"eyAiaHVtaWRpdHkiIDogNjguOSwgInRlbXBlcmF0dXJlIjogMjUuN30="}
and I would like use coloured data to visualization. "data" is Base64 ..
Thank you in advanced
[Removed duplicate post]
[Removed duplicate post]
The example JSON that you gave is not valid JSON (there's an extra } and a hanging ] that make it invalid), but if it were a valid JSON like below, then you could create a Thing with properties and a script in either a service or subscription to handle it.
{
"applicationID": "1",
"applicationName": "temperature-humidity",
"deviceName": "TH_sensor",
"time": "2019-08-26T14:31:54.833027Z",
"location": {
"latitude": 0,
"longitude": 0,
"altitude": 0
},
"data": "eyAiaHVtaWRpdHkiIDogNjguOSwgInRlbXBlcmF0dXJlIjogMjUuN30="
}
In a Thing or Thing Template, create properties to match your desired data:
| Property Name | Type |
| applicationName | String |
| humidity | Number |
| location | Location |
| temperature | Number |
| time | DateTime |
Then you can make a Service or Subscription that uses built-in JavaScript functions to parse out the data for you:
/* This script parses an inputData JSON string to individual Thing Properties */ // decode the Data field from base64, then make it a JSON object var decodedData = JSON.parse( base64DecodeString(inputData.data) ); // pull those decoded + parsed values out me.temperature = decodedData.temperature; me.humidity = decodedData.humidity; // set the rest of the items from the inputData to the Thing Properties me.applicationName = inputData.applicationName; me.time = inputData.time; me.location = inputData.location; me.location.elevation = inputData.location.altitude; // note that this *could* be set in the previous line if the incoming data used "elevation" instead of "altitude" for the key
There are more resources here on Services, Events, and Subscriptions: https://developer.thingworx.com/en/resources/learning-paths/monitor-factory-supplies-and-consumables/data-model-services-events-and-subscriptions
