Jump to content

Recommended Posts

  • 2 weeks later...

 

On 8/5/2021 at 10:29 AM, drjdpowell said:

Consider just not including those parameters.  Rather than {"A":123,"B":null,"C":789} just have {"A":123,"C":789}; then "B" will be default.

Alternately you could put named-Variant values inside your variants (which teh Variant-to-Data node will pass through:

1090371450_2021-08-0517_26_42-Untitled2BlockDiagramonJSONtext.lvproj_MyComputer_.png.63907e2224240eac829f7dadb8bfa9d8.png

Variants are quite tricky, as they can serve both as a temporary container for a value, and a value itself.

This is opposite of what I would expect to see. Fundamentally if I give you a cluster with named items (who cares about the data type), I expect the JSON output to an object with fields matching the names of the clusters. All of this "variants have names" discussion is optimizing of weird LabVIEW shenanigans instead of a primary use case.

 

On 8/5/2021 at 10:19 AM, Aristos Queue said:

Allow me to rephrase then.

Recording an array when passed a NAMED cluster is definitely a bug, regardless of the behavior of the variants.

+1

Link to comment

The following VIs aren't reentrant but should be:

- JSONtext.lvlib:Format JSON Array Text.vi
- JSONtext.lvlib:Parsing Error.vi
- JSONtext.lvlib:Format JSON Object.vi
- JSONtext.lvlib:Get all Object Items.vi
- JSONtext.lvlib:Get Array Elements.vi
- JSONtext.lvlib:Parsing Error in converting to LabVIEW type.vi

Link to comment

Does anyone actually use Variants in clusters in this way?  To me, this is not even a tertiary use case of JSONtext, so it would be helpful to hear from people with real-world uses.  I am tempted to suggest throwing a "Variants not supported" error, but even that would add overhead which I am loath to do.

Link to comment
20 minutes ago, drjdpowell said:

Does anyone actually use Variants in clusters in this way?  To me, this is not even a tertiary use case of JSONtext, so it would be helpful to hear from people with real-world uses.  I am tempted to suggest throwing a "Variants not supported" error, but even that would add overhead which I am loath to do.

I've used them in that way pre-maps leveraging variant attributes. Don't expect JSONtext to handle that use case, though.

Link to comment
  • 1 month later...
15 hours ago, Michael Aivaliotis said:

is it possible to improve the error handling for empty strings? Perhaps have a special error code I can filter on if an empty string is detected?

 

image.png

Use case?   Why do you need different custom error than the 402841: "Invalid JSON formatting" that you have now?

Link to comment
On 10/11/2021 at 1:34 AM, Michael Aivaliotis said:

is it possible to improve the error handling for empty strings? Perhaps have a special error code I can filter on if an empty string is detected?

How about wiring the string into a case structure selector?

  • Case "": Do your special output
  • Case Default: Wire the string into the JSON VIM
Link to comment

Yes, empty string is invalid JSON, but so could be malformed text, which is more serious than an empty string which could imply reading a non-existent JSON item.

How should I handle reading JSON files where data elements are missing? I like having the default data input so if an item is missing, it returns the default, of course. But now I have to write special code for every node. What is the best practice when using this toolkit for this use-case?

I guess I could create custom wrappers. But I wanted to avoid the extra work for something that's common.

Am I using it wrong?

Link to comment
5 hours ago, Michael Aivaliotis said:

How should I handle reading JSON files where data elements are missing?

Missing as in "field does not exist in the object", or missing as in "field exists but the value is an empty string"?

  • The latter is not truly missing. It exists, and its value is an empty string.
  • The former can be handled using the "Data Type and Default Value" inputs

1344596818_MissingJSON.png.6295ac54b049d6fa13467c4b182582cb.png1456594771_MissingJSONOutput.png.d52056df16414281a474f4bcf14469db.png

 

5 hours ago, Michael Aivaliotis said:

an empty string which could imply reading a non-existent JSON item.

Sounds like you're using "Find Item.vim"? Make use of the "Found" output, OR switch to "Find Item (as LVtype).vim" to specify the default value.

 

6 hours ago, Michael Aivaliotis said:

I guess I could create custom wrappers. But I wanted to avoid the extra work for something that's common.

How do you envision providing the default value if you are able to filter out the proposed "Empty String" error?

Link to comment

JKSH has explained as well as I could the expected use.  "Missing from Object" is determined when operating on the Object.  

I had a look, and it should be reasonably easy to give a different error for a string with only whitespace, but I'll need to understand use case for it better.  I, personally, never find the need to write custom error-handling code.

By the way, if one's use case is "set from this JSON as much as possible, or use supplied default for items where this is not possible", then one can just not use the error wires at all.

Edited by drjdpowell
Link to comment
  • 1 month later...

Hi, I am trying to filter an Array like this:

 

[    {
    "folder" : "User/Admin/UserA",
    "User" :  "Linda"
},
 {
    "folder" : "User/Service/UserB",
    "User" :  "John"
},
{
    "folder" : "User/Admin/UserC",
    "User" :  "Peter"
} ]

 I want to get a Array with all Clusters where folder contains "Admin"

This $Path gives me one result in my case.
$..[?(@.folder=="User/Admin/UserA")]

How can I receive all folders? Something like  $..[?(@.folder=="User/Admin/*")] isn't working. Are regEx operators (=~) or the query command contains not working?

Thank you,

Albert

Link to comment
2 hours ago, SahnAlbert said:

I want to get a Array with all Clusters where folder contains "Admin"

I don't have the filtering syntax of JSONpath yet implemented in JSONtext.  In fact, you surprised me by seeming to indicate that $..[?(@.folder=="User/Admin/UserA")] actually works, when it doesn't.  At some point I may have teh time to implement that, but in the meantime consider filtering code in LabVIEW like this

 

2021-11-30 08_45_02-Untitled 2 Block Diagram on Untitled Project 1_My Computer _.png

Link to comment
  • 4 months later...
  • 3 months later...
On 1/30/2020 at 2:07 PM, drjdpowell said:

The error is coming from where you attempt to convert null (as in "comment":null) to a string.   Intuitively, one would think that a null should be equivalent to an empty string (just as when converted to a number, null becomes NaN and does not throw an error).  Thank you for the report.

I'm running into this same issue.  A web server returns
"key":null

When trying to use JSONtext to convert JSON to LabVIEW data, I get the error:
Unable to convert the following JSON to LVType 'String':null

I would love for "null" to return an empty string.
Is there a workaround here that anyone has been implementing?

I'm doing a string replace for now, but this is hacky and not bulletproof:

image.png.152d51933725d8a7a77e873c62d8fd66.png

Link to comment

I have several instances where I get null back for different datatypes, what I have to do is just to ignore the Errors from the JSON VIs.
So it will convert everything it can' but when trying to convert NULL to a string it will be empty or for an int it will be zero.
Ideally it would be good to be able to say when you find Null for:
A strings use "" or "NULL"
A INT use 0 or -1

For DBL/SGL it uses NaN and that is prefect.

  • Thanks 1
Link to comment

The hardest to figure out will be booleans which in JSON can be true, false or NULL.

While you can get away with converting null to NAN for output to LabVIEW, be aware that NAN is specifically excluded for encoding as a numeric.

Quote

Numeric values that cannot be represented as sequences of digits (such as Infinity and NaN) are not permitted.

RFC4627 section 2.4

Edited by ShaunR
Link to comment
  • 9 months later...

The Read Config from File.vi implies that it will use the Template if the file does not exist. Using the example "JSONtext Config Example Application 1.vi", I deleted the config file to see if it would indeed create it using the Template. Running the example creates the file, but its blank. Is the Template used for creating the file or for generating the "<JSON> Config" output?

image.png.e814059e529aad264f8d1f3920c24e99.png

Link to comment

Join the conversation

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

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

×
×
  • Create New...

Important Information

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