From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using LabVIEW to compose JSON strings incrementally

Solved!
Go to solution

I'm struggling to figure out a way to build JSON representations of objects incrementally in LabVIEW. By this I mean I want to split the generation of a complete JSON document across separate VIs in order to keep my code DRY.

 

In particular, I'm creating an application in LabVIEW that needs to communicate with a webservice. This webservice expects to receive JSON patch operations (see RFC 6902), which means JSON objects with the following schema:

 

{
  "op": <enumeration: add, replace, etc.>,
  "path": <string>,
  "value": <arbitrarily complex object>
}

 

It makes sense to create one VI that accepts a "path" string and something (a variant? Another JSON string?) describing the "complex object" and generates the JSON patch operation. This would eliminate the need to duplicate this logic across several VIs.

 

As in many cases, this is something that's straightforward to do in most languages but, at least for me, not in LabVIEW. I've posted a couple of snippets below to illustrate what I mean better.

 

In the example below, I want to create a JSON patch operation that says "add a new transmit message definition under "testcan.candb" with the name "example_tx_msg"":

 new_tx_msg_example.png

 

In this example, I want to create another JSON patch operation that says "add a field to that message definition called "example_tx_field"":

new_tx_field_example.png

 

These VIs do not work as intended, however, which makes sense because I'm passing the JSON representation of a complex object to "value" rather than the object itself. I expect to get JSON like this:

{
	"op": "add",
	"path": "testcan.candb.example_tx_msg",
	"value": {
		"type": "tx",
		"messageId": 540,
		"repeatRate": 100,
		"canPort": 0,
		"template": [255, 1]
	}
}

I actually get JSON like this:

{
	"op": "add",
	"path": "testcan.candb.example_tx_msg",
	"value": "{\"type\":\"tx\",\"messageId\":540,\"repeatRate\":100,\"canPort\":0,\"template\":[255,1]}"
}

 

I'd very strongly prefer a solution to this to use only native LabVIEW VI's. That's because:

  • Unlike pretty much every other package management system, the de facto one for LabVIEW (VIPM) charges you to use half its features.
  • In any case, VIPM fails to install any OpenG package for me (HTTP 400: bad request), and by extension any packages depending on these (such as JKI's JSON API package).

 

0 Kudos
Message 1 of 12
(8,810 Views)
Solution
Accepted by topic author DavidFCannock

hi david!

 

the thing is, that when you try to create json from your cluster,

you already have the first as string, this is exactly what you get in your resulting JSON.

 

replace the "value" in second cluster with a variant,

then use JKI JSON library (from vipm), which lets you convert anything to json (the labview json converter, won't convert enums for example)

 

convert-to-json.pngconvert-to-json--output.png


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
Message 2 of 12
(8,772 Views)

Hi jwscs,


 

the thing is, that when you try to create json from your cluster,

you already have the first as string, this is exactly what you get in your resulting JSON.

 


I'm aware, that's why I wrote "These VIs do not work as intended, however, which makes sense because I'm passing the JSON representation of a complex object to "value" rather than the object itself."

 

I was trying to convey the gist of what I wanted with a setup that I knew didn't work but was close enough.

 


replace the "value" in second cluster with a variant

 


This was the first thing I tried, but it doesn't seem to work with "Flatten To JSON"



then use JKI JSON library (from vipm), which lets you convert anything to json (the labview json converter, won't convert enums for example)

 

 


As I mentioned in my post, I was trying to avoid using third-party libraries for this. In particular, this is what happens when I try to install JKI's JSON library using VIPM 2017:

LabVIEW_2017-08-23_11-24-58.png

Still, I'm grateful you provided the solution you did. It's nice to know for sure that I can use JKI's JSON library if I ever get it installed.

0 Kudos
Message 3 of 12
(8,765 Views)

sorry completely ignored the last part 😞

 

EDIT:

in labview 2017 (which i do not use yet) there has been a change with the package manager, e.g. they integrated the jki one into labview.

it might be best to get the package manager working first and find out, if there are some caveats, like you can only use the internal one, but i have no knowledge about 2017 intricacies yet


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
0 Kudos
Message 5 of 12
(8,757 Views)

@jwscs wrote:

in labview 2017 (which i do not use yet) there has been a change with the package manager, e.g. they integrated the jki one into labview.

it might be best to get the package manager working first and find out, if there are some caveats, like you can only use the internal one, but i have no knowledge about 2017 intricacies yet


I searched Google, got nothing. Searched the LabVIEW forums, got nothing. Searched the JKI forums (dead as they are) and found this post from 11 years ago with someone apparently experiencing the same issue as me.

 

Quoting from that thread:

 

> Please, try changing your TCPIP timeout to something like 60 seconds (see this post for details) and see if that helps

It was already at 60 seconds. Bumped it to 120 seconds and it didn't help.

 

> We are aware of this issue and are working on a long term resolution.

 

I guess over a decade still counts as short-term. If I can get this package installed before I'm 50, I'm sure my boss would appreciate it.

 

My guess is that this is entirely a server-side issue, not client-side. If that's the case there's not much I can do on my end to resolve this. To actually confirm this is the case, I'd need to check whether other people are having any trouble installing OpenG packages. For the time being I might be able to manually locate and download the required packages, as the person in the thread did.

 

Honestly, I don't have a huge deal of faith in the JKI team at the moment. My intended deployment solution for my application was recently screwed over due to the fact that VIPM fails to build packages for projects containing malleable VIs - an issue that was reported months ago. This despite the fact that JKI explicitly claim they support malleable VIs in VIPM 2017. Generally when I release software advertising that it has Feature X, I like to first check that it actually does have Feature X.

0 Kudos
Message 6 of 12
(8,746 Views)

try to start a new post for the VPIM issue to get better visibility.

 

another thought, if vipm is integrated into labview, shouldn't NI sort you out now?


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
Message 7 of 12
(8,742 Views)

@jwscs wrote:

try to start a new post for the VPIM issue to get better visibility.

 

another thought, if vipm is integrated into labview, shouldn't NI sort you out now?


That's not a bad idea. I'll open a new service request about the VIPM issue as soon as I close the current one I have open with them.

0 Kudos
Message 8 of 12
(8,740 Views)

I resolved the problem with VIPM.

 

I tried installing the JSON package on another machine and found it worked. So I went back to mine and tried running my version of VIPM as admin and installing the package. That didn't work. I tried a full reinstallation of VIPM 2017, and that didn't work either. I managed to track down a configuration folder used by VIPM that's shared between different installation versions though, so I made a backup of it and then deleted it. When I tried running VIPM 2017, it essentially behaved like a fresh installation, although a lot differently than before. After that I was able to install the package without issue.

 

With the JSON library installed, I was able to try out your proposed solution and it works like a charm, so I'm marking it as accepted.

0 Kudos
Message 9 of 12
(8,712 Views)

congrats!


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
0 Kudos
Message 10 of 12
(8,708 Views)