From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, 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: 

JSON difficult to parse

Solved!
Go to solution

Hi all,

I tried to parse the JSON here under coming from thirdy part web service.

I was able to parse only the inner part using NI "unflatten from JSON". The main problem are:

 

1) this JSON is an array of different type objects

2) this JSON contain value without name

 

Every advice will be helpfull and thanks in advance

Golzio

 

[{
"name": "DC Comics",
"rev": [
[1389826800000, {
"Note": "Note Title 1",
"Vote": "EVEN",
"createdBy": "Batman",
"timeInMills": 1389826800000,
"detailNote": "This a detailed note"
}],
[1391122800000, {
"Note": "Note Title 2",
"Vote": "EVEN",
"createdBy": "Superman",
"timeInMills": 1391122800000,
"detailNote": "This a detailed note"
}],
[1392418800000, {
"Note": "Note Title 3",
"Vote": "EVEN",
"createdBy": "Wonderwoman",
"timeInMills": 1392418800000,
"detailNote": "This a detailed note"
}]
]
}, {
"name": "Marvel",
"rev": [
[1388530800000, {
"Note": "Note Title 3",
"Vote": "EVEN",
"createdBy": "Spiderman",
"timeInMills": 1388530800000,
"detailNote": "This a detailed note"
}]
]
}]

0 Kudos
Message 1 of 8
(17,922 Views)

The valid json to be unflattened is the following:

 

[{
"name": "DC Comics",
"rev": [
[1389826800000, {
"Note": "Note Title 1",
"Vote": "EVEN",
"createdBy": "Batman",
"timeInMills": 1389826800000,
"detailNote": "This a detailed note"
}],
[1391122800000, {
"Note": "Note Title 2",
"Vote": "EVEN",
"createdBy": "Superman",
"timeInMills": 1391122800000,
"detailNote": "This a detailed note"
}],
[1392418800000, {
"Note": "Note Title 3",
"Vote": "EVEN",
"createdBy": "Wonderwoman",
"timeInMills": 1392418800000,
"detailNote": "This a detailed note"
}]
]
}, {
"name": "Marvel",
"rev": [
[1388530800000, {
"Note": "Note Title 3",
"Vote": "EVEN",
"createdBy": "Spiderman",
"timeInMills": 1388530800000,
"detailNote": "This a detailed note"
}]
]
}]

0 Kudos
Message 2 of 8
(17,912 Views)

I was able to parse it using the "JSON toolkit for LabVIEW":

 

https://decibel.ni.com/content/groups/interactive-internet-interface-json-toolkit-for-labview?view=d...

 

Attached a sample VI.

 

If someone has good advice to parse using native NI vi please let me know.

 

Regards

Golzio

0 Kudos
Message 3 of 8
(17,888 Views)
Solution
Accepted by topic author Golzio

Hey Golzio,

 

The problem here is that the Unflatten from JSON primitive only makes it easy to parse JSON data that be well represented by LabVIEW's types. So cases like this where we have an array with mixed types are more difficult.

 

It is possible to parse JSON Arrays with mixed types using LabVIEW's primitives. This is done by using the path input on Unflatten from JSON to navigate into the JSON data and extract the values you need.

 

The following example shows how you can create paths to extract the data from your data set:

Parse JSON Array with Mixed Types using Unflatten From JSON.png

 

From that example you can see how we build the path array in loops and use Unflatten From JSON to extract values one-by-one. A problem with this approach is that we are calling the Unflatten from JSON many times inside a loop so this might not scale well for large data sets with thousands of items. The benefit is not requiring external dependencies.

 

I'm glad the i3 JSON toolkit worked for you (full disclosure I developed that specific toolkit) and it's good to know that there are other options are available such as the newer JKI JSON Toolkit and the existing LAVA JSON LabVIEW Toolkit


Milan
Message 4 of 8
(17,881 Views)

Hi Milan,

thank you very much for your time.

It was very useful to understand better and in deep the smart use of "path" input of "Unflatten form JSON".

 

Golzio

0 Kudos
Message 5 of 8
(17,830 Views)

I've just released a new library called JSONtext.  Part of the reason for a new library is to provide more complete support of JSON, including mixed-type arrays.  Here is a solution to the above problem, using JSONtext:

JSONtext example snippet.png

 In JSONtext, mixed-type JSON arrays can be unflattened to clusters of unnamed elements.

Message 6 of 8
(16,999 Views)

Hello drjdpowell,
I'm a student, and a begener on labview.
In my internship I receive data from a server in JSON and my job it's to recover this data and make some graphics in Labview.
So i was very happy to find your new library JSONtext so I have download it.
The problem is when I want to Use for exemple the VI From JSON text, labview start to download some files and after he ask me to open two VI:
- Array of Variants to Cluster.vi

- Trim Whitespace_JDP.vi
But I can't find this 2 VI....
Can you please helped me.

 

I have labview 2017
My JSON file look like this:

  [
{ "measurement": "ACPowerFromStorage", "value": { "dataGeohash": "u0p70yfrzmu1", "last": 0, "time": "2018-02-20T08:48:39Z" } }, { "measurement": "ACPowerToStorage", "value": { "dataGeohash": "u0p70yfrzmu1", "last": 990, "time": "2018-02-20T08:48:39Z" } }, { "measurement": "AmbientTemp", "value": { "dataGeohash": "u0p70yfrzmu1", "last": 0, "time": "2018-02-20T08:48:39Z" } },

 

Download All
0 Kudos
Message 7 of 8
(16,382 Views)

I found the problem I forgot to download the package: jdp_science_lib_common_utilities
Problem solved

0 Kudos
Message 8 of 8
(16,373 Views)