cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

Parse JSON string and decode Base64 data

ZAJO
4-Participant

Parse JSON string and decode Base64 data

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

3 REPLIES 3
eliotlandrum
14-Alexandrite
(To:ZAJO)

[Removed duplicate post]

eliotlandrum
14-Alexandrite
(To:ZAJO)

[Removed duplicate post]

eliotlandrum
14-Alexandrite
(To:ZAJO)

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

Announcements


Top Tags