ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

JSON parsing error (Variant To Data in i3-json.lvlib:Get Object Parameter - String.vi)

Hi,

 

I'm using the i3 json toolkit for LabVIEW : https://forums.ni.com/t5/JSON-Toolkit-for-LabVIEW/gp-p/8520?start=0&view=discussions

 

I'm facing an error when using the json below and trying to get the "cmd" value :

 

{"cmd":{"module":"test1","name":"Session"}}

 

Get Object parameter VI returns :

Variant To Data in i3-json.lvlib:Get Object Parameter - String.vi->RS_GetJsonElement.vi

 

This case seems basic so I might be mistaking somehow.

 

Does any one get the same issue using this library, and is there a way to fix it?

 

Thanks in advance for your support,

 

Bim

 

 

 

0 Kudos
Message 1 of 7
(6,072 Views)

Attached vi snapshot

0 Kudos
Message 2 of 7
(6,069 Views)

Hi Bim,

 

In the example JSON provided it can be thought of as follows:

 

A JSON object with one parameter. That parameter is named cmd.

The cmd parameter is a JSON object with two parameters. The parameters are module and name.

The module parameter is a string with value "test1".

The name parameter is a string with value "Session".

 

In the VI screenshot the parameter cmd was read as a String which returned an error because the cmd parameter is a JSON object. To extract the cmd parameter you have to read it as a JSON object. To then read module and name parameters you have to look them up from the cmd JSON object:

 

parsingjson.png


Milan
0 Kudos
Message 3 of 7
(6,040 Views)

If you are using LabVIEW 2013 or greater you may also consider using the Unflatten From JSON primitive built into LabVIEW instead:

 

unflattenfromjson.png

 

Unflatten from JSON generally gives better performance and can be easier to use in many cases.


Milan
0 Kudos
Message 4 of 7
(6,036 Views)

Hi Milan,

 

Many thanks for your feedbacks. Actually I was expecting it to manage both cases and result a string that could be the flattened json as string.

I'll build for the moment a specific vi for my basic need as this dependency on the input json form is blocker.

 

Thanks,

 

BR,

 

Bim

0 Kudos
Message 5 of 7
(6,027 Views)

You can use the Encode JSON vi to serialize a JSON Object back into a string encoded as JSON. The following shows how the cmd parameter is retrieved and serialized into a JSON string.

 

convertbacktojson.png

 

convertbacktojsonfrontpanel.PNG


Milan
0 Kudos
Message 6 of 7
(6,021 Views)

Hi Milan,

 

Great example, I was missing something and now it's clear.

 

Many thanks for your support!

 

Bim

0 Kudos
Message 7 of 7
(5,993 Views)