LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Decoding JSON

Solved!
Go to solution

Hello,

I have a JSON reply from a device and need to decode a number of values from it that correspond to 4 values from 6 analog channels. I have looked and tried to work with the examples but needed a little bit more help if anyone can. 
Thank you in advance
Lee

0 Kudos
Message 1 of 21
(2,671 Views)

Hi Lee,

 

which kind of problems do you got?

 

The straight forward way is to use MatchPattern/ScanFromString several times on your JSON text. (Search for "channelData", then search for "id", then scan for needed values…)

 

Other options include using one of the available JSON toolkits to parse the object…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 21
(2,656 Views)

Hello,

Thank you - I understand both of your approaches. I dont have a toolkit and the first approach seems the wrong approach and messy to me, am i not able to make use of the unflatten from JSON function? or would that not be enough?

0 Kudos
Message 3 of 21
(2,642 Views)

Here's a possible solution.

Note that you have to take care when defining the input type.

demo.png


GCentral
Message 4 of 21
(2,635 Views)

Hello

I can see where you are going with this, thank you.

My problem is working out how to construct the cluster as i am not sure what are names or values or what type of data is coming. If that makes sense

Lee 

0 Kudos
Message 5 of 21
(2,608 Views)

Hello, 

OK so that is great i can see how to decode the output now - fantastic, thank you.
I have a similar issue now going the other way - sending a JSON command

This is the data I have on it, i wondered if you can help with this also?

 

{"jsonrpc":"2.0", "id":0, "method":"setAlarms",
"params":{
"alarms":[
{“channel”:0,”levelHigh”:300},
{“channel”:1,”levelHigh”:300},
...
]
}
}
Response when ok:
{"jsonrpc":2.0,"id":0,"result":null}

 

 

0 Kudos
Message 6 of 21
(2,595 Views)

JSON.png

Not sure if\how the null can be done. But you can always use a string constant "{"jsonrpc":2.0,"id":0,"result":null}" as it's a static reply.

0 Kudos
Message 7 of 21
(2,551 Views)

Fantastic thank you, I can structure the message now to send to the device.
One final last 'thing' is that the levelHigh number needs to be floating point but after I go through the json function it displays 22 but i need it to show 22.0 (i have changed the levelHigh number format but it doesnt go through)/

0 Kudos
Message 8 of 21
(2,533 Views)

There's no control over that with the build-in LV JSON tools. And it shouldn't matter. "22" is still "22.0" when you read it as a double. I'm not sure if other tools allow specifying this.

 

The only way you can force the 22.0 in the string, is to make a reg.ex. to replace that. But AFAIC, you shouldn't need to.

 

The reg.ex. won't be trivial...

0 Kudos
Message 9 of 21
(2,527 Views)

Yea, the device manufacturer are telling me it has to be floating point. If i manually manipulate the resulting string from the function the device responds correctly. 

Not sure where to go from here now

0 Kudos
Message 10 of 21
(2,521 Views)