Jump to content

Extracting values from JSON


Shmee
 Share

Recommended Posts

 

Hello,

I am having difficulties with extracting JSON messages and since this is my first time to deal with JSON extracting in LabVIEW, I am not sure whether it is possible to solve it and how.

Now, JSON input is something like this:


{"VALUE1":{"0":{"x":100,"y":0.2},"1":{"x":300,"y":0.4},"2":{"x":500,"y":0.6}},"VALUE2":{"0":{"x":1000,"y":0.200},"1":{"x":300,"y":0.4},"2":{"x":500,"y":0.6}},"VALUE2":{"0":{"x":10000,"y":0.2000},"1":{"x":3000,"y":0.04},"2":{"x":5000,"y":0.06}},"VALUE2":{"0":{"x":1000,"y":0.200},"1":{"x":300,"y":0.4},"2":{"x":500,"y":0.6}},"VALUE3":{"0":{"x":1000,"y":0.200},"1":{"x":300,"y":0.4},"2":{"x":500,"y":0.6}},"VALUE5":{"0":{"x":1000,"y":0.200},"1":{"x":300,"y":0.4},"2":{"x":500,"y":0.6}}}

You can see that I can have mixed object items: one "value1", repeated (multiple) "value2", one "value3", missing "value4" and then "value5"...

Any object item can be multiplied or missing in different messages I receive.

My question is how to extract repeated object items and plot them on a XY graph? To be more precise, if I get no item in a message - I should keep the old values (not to have drop to zero on a graph), if I get one item, I should just plot it and if I get multiple items in a message, I should plot all of them on a graph.

This is what I did so far. It plots and keeps old values if nothing received but I have problem extracting multiple values.

 

I would appreciate any hint or help.

image.thumb.png.aa25d07dd52764d9581c55a086796110.png

Thanks in advance,

Misa.

Link to comment
Share on other sites

  • 2 weeks later...

I might try an approach where you first get all object items as JSON strings.

Since they are all the same type (having three x, y coordinates), you can then iterate over the JSON Items (array of strings) and convert each of them to LabVIEW types "From JSONtext". 

Does this approach make sense?

image.png

Link to comment
Share on other sites

Hello Jim,


Thank you for response and effort. I've actually tried before similar approach but eventually gave up because of another problem. Maybe you could help solving that.


In order to plot on different graphs, I have to separate the output array into several arrays - separate array for every value. 
But as the JSONs on input differ from message to message, things are getting a bit more complicated because array size and positions of the values are constantly changing. The only solution I could think of is using index array function and some logic based on Item Names output from Get All Object Items VI to extract data from one combined array that we have in current VI. I guess I would have to copy that method for every value so it would be pretty nasty and I don't have a good feeling regarding optimization using method like that? Maybe there is a more elegant solution for that?
In attachment, you have the VI in which I have simulated three different JSON messages I could expect on input, going into the code you have suggested. Would be grateful if you could take a look.

Thanks in advance.

JSON Test.vi

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.