un-escaped json worked from pubnub but not Outsystems own generated escaped json

un-escaped json worked from pubnub but not Outsystems own generated escaped json

  

My parking sensor generates the following pubnub to a channel.

{'Sensor_text': 'S-001', 'Status_text':'0',  'Distance_nr': 10}

I have a mobile page with a listener subscribing to the same channel.

It receives the event, de-serialize the information and works fine

I can see though while debugging that the Javascript was used internally

{"Sensor_text": "S-001", "Status_text":"0",  "Distance_nr": 10}


Then I needed based on the same JSON structure - generate and simulate the JSON messages and publish these as events to pubnub.

I set the attributes and data in a record based on the structure - I pass it with the object to pubnub.

Pubnub handles the data as usual.... but

my mobile pages with the listener subscribing to the data gets the data escaped with " ..." around it

"{\"Status_text\":\"2\",\"Sensor_text\":\"S-001\"}"

And this breaks the de-serialization logic.


I "solved"it by

- creating an if statement that checks if 2nd character is { ... then substr remove first " ... then substr remove last " .. then replace \" with " -> now it looked the same and worked.

Is there a better way to handle this ?

Hi,


Can you share an oml, because i do not understand it entirely.

It should not needed to handle it in a custom way.


Greetings,

Hmm, maybe the definitions don't match. How did you created the Structure in OutSystems? From a Consume REST, the "Add Structure from JSON" command, or manually? The first two options are the easiest ways to guarantee there's no problem when doing JSON (de)serialization.

If you did that and the error still occurs, please check the "Name in JSON" property of the structure attributes if is correct:

Cheers.

Tommy Hägvall wrote:

My parking sensor generates the following pubnub to a channel.

{'Sensor_text': 'S-001', 'Status_text':'0',  'Distance_nr': 10}

I have a mobile page with a listener subscribing to the same channel.

It receives the event, de-serialize the information and works fine

I can see though while debugging that the Javascript was used internally

{"Sensor_text": "S-001", "Status_text":"0",  "Distance_nr": 10}


Then I needed based on the same JSON structure - generate and simulate the JSON messages and publish these as events to pubnub.

I set the attributes and data in a record based on the structure - I pass it with the object to pubnub.

Pubnub handles the data as usual.... but

my mobile pages with the listener subscribing to the data gets the data escaped with " ..." around it

"{\"Status_text\":\"2\",\"Sensor_text\":\"S-001\"}"

And this breaks the de-serialization logic.


I "solved"it by

- creating an if statement that checks if 2nd character is { ... then substr remove first " ... then substr remove last " .. then replace \" with " -> now it looked the same and worked.

Is there a better way to handle this ?

Sorry to bump an old post, but i am having a similar issue. would you be willing to share (give and example) how you got it to work?


thanks

In the PubNubListenerOnMessage Action:

I have a Message input parameter based on Message Data Structure 

Assign variable:

MessageText = Message.Message

( incomming message that we need to check if unescaped or not )


If statement

SyntaxEditor Code Snippet

Substr(MessageText,1,1) = "{"

If True

Assign variable:

MessageText =

SyntaxEditor Code Snippet

Substr(MessageText,1,Length(MessageText)-2)

And then JSON DeserializeSensor ( Sensor_reading datastructure )


If False

And then JSON DeserializeSensor ( Sensor_reading datastructure )


It works fine with both unescaped and escaped