Copyright © https://mongoose-os.com

Mongoose OS Forum

frame
ATTENTION! This forum has moved to:

https://community.mongoose-os.com

Do not post any new messages.

strange behaviour of JSON.parse

I experience strange JavaScript behavior

My init.js file is attached

When variable rmsg receives string 1 or 0 from MQTT . JSON.parse(rmsg) converts it to strange number
My log shows

Topic: butas/esp1/relays/1 message: 0 
[Nov 11 13:53:29.568]  set relay 1  =  2256   pin  =  15 
[Nov 11 13:53:32.108]  Topic: butas/esp1/relays/1 message: 1 
[Nov 11 13:53:32.119]  set relay 1  =  12256   pin  =  15 
[Nov 11 13:53:33.087]  Topic: butas/esp1/relays/1 message: 0 
[Nov 11 13:53:33.095]  set relay 1  =  2256   pin  =  15 
[Nov 11 13:53:34.180]  Topic: butas/esp1/relays/1 message: 1 
[Nov 11 13:53:34.188]  set relay 1  =  12256   pin  =  15 
[Nov 11 13:53:35.195]  Topic: butas/esp1/relays/1 message: 0 
[Nov 11 13:53:35.203]  set relay 1  =  2257   pin  =  15 
[Nov 11 13:53:51.527]  Topic: butas/esp1/relays/1 message: 1 
[Nov 11 13:53:51.535]  set relay 1  =  12222   pin  =  15

if I disable this part of code by commenting it

let acs_val = ADC.read(acs_pin);
let acstopic = topic+'/current/'+JSON.stringify(acs_pin);
let acs_val_str = JSON.stringify(acs_val);
let ok = MQTT.pub(acstopic, acs_val_str, 1);

it start parsing correctly

Topic: butas/esp1/relays/1 message: 0 
[Nov 11 13:58:11.880]  set relay 1  =  0   pin  =  15 
[Nov 11 13:58:12.249]  Topic: butas/esp1/relays/1 message: 1 
[Nov 11 13:58:12.266]  set relay 1  =  1   pin  =  15 
[Nov 11 13:58:12.754]  Topic: butas/esp1/relays/1 message: 0 
[Nov 11 13:58:12.768]  set relay 1  =  0   pin  =  15 
[Nov 11 13:58:13.043]  Topic: butas/esp1/relays/1 message: 1 
[Nov 11 13:58:13.057]  set relay 1  =  1   pin  =  15 
[Nov 11 13:58:13.373]  Topic: butas/esp1/relays/1 message: 0 
[Nov 11 13:58:13.386]  set relay 1  =  0   pin  =  15 
[Nov 11 13:58:13.735]  Topic: butas/esp1/relays/1 message: 1

What is also strange that removing comments will allow it to work for some time until some strange point and then goes mad again

[Nov 11 13:57:17.047]  set relay 1  =  1   pin  =  15 
[Nov 11 13:57:17.281]  Topic: butas/esp1/relays/1 message: 0 
[Nov 11 13:57:17.289]  set relay 1  =  0   pin  =  15 
[Nov 11 13:57:17.712]  Topic: butas/esp1/relays/1 message: 1 
[Nov 11 13:57:17.721]  set relay 1  =  1   pin  =  15 
[Nov 11 13:57:18.386]  Topic: butas/esp1/relays/1 message: 0 
[Nov 11 13:57:18.398]  set relay 1  =  0   pin  =  15 
[Nov 11 13:57:18.880]  Topic: butas/esp1/relays/1 message: 1 
[Nov 11 13:57:18.889]  set relay 1  =  12256   pin  =  15 
[Nov 11 13:57:19.328]  Topic: butas/esp1/relays/1 message: 0 
[Nov 11 13:57:19.336]  set relay 1  =  2256   pin  =  15 
[Nov 11 13:57:19.863]  Topic: butas/esp1/relays/1 message: 1 
[Nov 11 13:57:19.871]  set relay 1  =  12256   pin  =  15 
[Nov 11 13:57:20.265]  Topic: butas/esp1/relays/1 message: 0 
[Nov 11 13:57:20.273]  set relay 1  =  2256   pin  =  15

Does anybody have an idea what is wrong ?

Comments

  • Could you please craft a minimal example when JSON.parse() fails?

  • CipanXCipanX Malaysia

    did u managed to solve this?

  • mamuespmamuesp Germany/Northern coast

    The error has to be searched in the surrounding code, not the parsing function. It's the principle "garbage in, garbage out". At first you have to look, what data is passed to 'JSON.parse'. I doubt very strongly that the parsing function is to blame.

Sign In or Register to comment.